Die Betriebskontrolle im Testbetrieb meldete Stundendateien als fehlend, die es gab: 105 Eintraege in der Reihenfolge des Dateisystems, vierzeilig als eingeruecktes JSON. list liefert jetzt die 50 zuletzt geaenderten Eintraege, neueste zuerst, eine Zeile je Eintrag, mit Gesamtzahl im Kopf; pattern, sort, order und limit steuern das. Dabei aufgefallen (#675): Directory.GetFiles wertet Trennzeichen im Muster aus - pattern "../*" fuehrte an der Pfadpruefung vorbei aus dem Verzeichnis heraus, in list wie in read_many. Muster mit '/', '\' oder '..' werden jetzt abgewiesen. Und derselbe Befund ist keine zweite Aufgabe (#674): task_find beantwortet die Frage vor dem Anlegen, task_create legt bei gleichbedeutendem Titel einer offenen Aufgabe keine zweite an, sondern vermerkt das erneute Auftreten an der bestehenden. force=true geht trotzdem, Erledigtes blockiert nichts. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1031 lines
48 KiB
C#
1031 lines
48 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using ClawdDotNet.Core.Storage;
|
|
using ClawdDotNet.Core.Tools;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace ClawdDotNet.Tools.FileRW;
|
|
|
|
public sealed class FileRWTool : IAgentTool, IToolStateProbe
|
|
{
|
|
/// <summary>
|
|
/// Per-Datei Locking: Verhindert Race Conditions wenn mehrere Agenten
|
|
/// gleichzeitig auf dieselbe Datei zugreifen (read/write/append/delete/copy).
|
|
/// Key = normalisierter absoluter Pfad (lowercase), Value = SemaphoreSlim(1,1).
|
|
/// </summary>
|
|
private static readonly ConcurrentDictionary<string, SemaphoreSlim> FileLocks = new();
|
|
|
|
private static SemaphoreSlim GetFileLock(string fullPath)
|
|
=> FileLocks.GetOrAdd(NormalizePath(fullPath), _ => new SemaphoreSlim(1, 1));
|
|
|
|
private static string NormalizePath(string path)
|
|
=> Path.GetFullPath(path).ToLowerInvariant();
|
|
|
|
public string Name => "FileRW";
|
|
|
|
public string Description => "Dateiverwaltung in zwei getrennten Workspace-Verzeichnissen:\n" +
|
|
"- workspace='personal': Dein PRIVATER Ordner — nur du hast Zugriff.\n" +
|
|
"- workspace='shared': GETEILTER Ordner — alle Agenten im Team können darauf zugreifen.\n" +
|
|
"Aktionen: read, read_many, write, append, list, delete, copy, stock_add.\n" +
|
|
"'read_many' liest mehrere Dateien in EINEM Aufruf - entweder 'paths' (Liste) oder 'path' " +
|
|
"(Verzeichnis) mit 'pattern' (z.B. \"berlin_2026-09-20_*.txt\"). Nimm das, statt viele " +
|
|
"Dateien einzeln zu lesen: jeder Einzelaufruf kostet einen Schritt.\n" +
|
|
"'list' zeigt standardmäßig die 50 zuletzt geänderten Einträge, neueste zuerst; " +
|
|
"'pattern', 'sort', 'order' und 'limit' steuern das. Frage nach einer bestimmten Datei " +
|
|
"lieber mit 'pattern', statt eine lange Liste zu durchsuchen.\n" +
|
|
"Bei copy kann zwischen Workspaces kopiert werden.\n" +
|
|
"'stock_add' fügt einen neuen Datenpunkt zur Aktien-Wissensdatenbank hinzu (shared:stocks/{ticker}/). " +
|
|
"Geschützte Pfade (protectedPaths) erlauben nur das Erstellen neuer Dateien — kein Überschreiben oder Löschen.";
|
|
|
|
public JsonElement InputSchema { get; } = JsonDocument.Parse("""
|
|
{
|
|
"type": "object",
|
|
"properties": {
|
|
"action": {
|
|
"type": "string",
|
|
"enum": ["read", "read_many", "write", "append", "list", "delete", "copy", "stock_add"],
|
|
"description": "Die auszuführende Aktion. 'read_many' liest mehrere Dateien auf einmal. 'stock_add' fügt einen strukturierten Datenpunkt zur Aktien-Wissensdatenbank hinzu."
|
|
},
|
|
"workspace": {
|
|
"type": "string",
|
|
"enum": ["personal", "shared"],
|
|
"description": "In welchem Workspace die Aktion ausgeführt werden soll (Quell-Workspace bei copy). Standard ist 'personal'."
|
|
},
|
|
"path": {
|
|
"type": "string",
|
|
"description": "Relativer Pfad zur Datei oder zum Verzeichnis"
|
|
},
|
|
"content": {
|
|
"type": "string",
|
|
"description": "Inhalt für write oder append Aktionen"
|
|
},
|
|
"paths": {
|
|
"type": "array",
|
|
"items": { "type": "string" },
|
|
"description": "Nur für read_many: die zu lesenden Dateien. Alternativ 'path' (Verzeichnis) mit 'pattern'."
|
|
},
|
|
"pattern": {
|
|
"type": "string",
|
|
"description": "Für read_many und list: Dateimuster im Verzeichnis 'path', z.B. 'berlin_2026-09-20_*.txt'. Ohne Muster gilt das ganze Verzeichnis."
|
|
},
|
|
"sort": {
|
|
"type": "string",
|
|
"enum": ["modified", "name"],
|
|
"description": "Nur für list: Sortierung. Standard 'modified' (neueste zuerst)."
|
|
},
|
|
"order": {
|
|
"type": "string",
|
|
"enum": ["desc", "asc"],
|
|
"description": "Nur für list: Richtung. Standard absteigend bei 'modified', aufsteigend bei 'name'."
|
|
},
|
|
"limit": {
|
|
"type": "integer",
|
|
"description": "Nur für list: Höchstzahl der Einträge. Standard 50, Maximum 500. Der Kopf der Antwort nennt immer die Gesamtzahl."
|
|
},
|
|
"destinationWorkspace": {
|
|
"type": "string",
|
|
"enum": ["personal", "shared"],
|
|
"description": "Ziel-Workspace für copy. Standard ist gleicher Workspace wie 'workspace'."
|
|
},
|
|
"destinationPath": {
|
|
"type": "string",
|
|
"description": "Relativer Zielpfad für copy. Pflichtfeld bei copy."
|
|
},
|
|
"ticker": {
|
|
"type": "string",
|
|
"description": "Aktien-Ticker für stock_add (z.B. 'NVDA', 'TSLA'). Wird automatisch zu Großbuchstaben."
|
|
},
|
|
"category": {
|
|
"type": "string",
|
|
"enum": ["news", "social", "analysis", "capitol", "price", "earnings", "filing", "sentiment", "other"],
|
|
"description": "Kategorie des Datenpunkts für stock_add."
|
|
},
|
|
"source": {
|
|
"type": "string",
|
|
"description": "Quelle des Datenpunkts für stock_add (z.B. 'reuters', 'x_unusual_whales', 'youtube', 'reddit')."
|
|
},
|
|
"title": {
|
|
"type": "string",
|
|
"description": "Kurze Überschrift/Zusammenfassung des Datenpunkts für stock_add."
|
|
},
|
|
"data": {
|
|
"type": "object",
|
|
"description": "Strukturierte Daten des Datenpunkts für stock_add. Kann beliebige Felder enthalten."
|
|
}
|
|
},
|
|
"required": ["action"]
|
|
}
|
|
""").RootElement.Clone();
|
|
|
|
public async Task<ToolResult> ExecuteAsync(
|
|
JsonElement input,
|
|
AgentToolContext context,
|
|
CancellationToken ct)
|
|
{
|
|
var action = input.GetProperty("action").GetString()
|
|
?? throw new ArgumentException("'action' is required");
|
|
|
|
var workspace = (input.TryGetProperty("workspace", out var w) ? w.GetString() : null) ?? "personal";
|
|
|
|
var rootPath = workspace == "shared" ? context.SharedWorkspacePath : context.WorkspacePath;
|
|
|
|
if (string.IsNullOrWhiteSpace(rootPath))
|
|
{
|
|
var wsName = workspace == "shared" ? "SharedWorkspace" : "PersonalWorkspace";
|
|
return ToolResult.Fail($"Konfigurationsfehler: '{wsName}' ist nicht definiert.");
|
|
}
|
|
|
|
// Sicherstellen, dass rootPath absolut ist
|
|
rootPath = Path.GetFullPath(rootPath);
|
|
|
|
// Zugriffsprüfung
|
|
if (!IsActionAllowed(action, workspace, context))
|
|
{
|
|
return ToolResult.Fail($"Zugriff verweigert: Die Aktion '{action}' ist im Workspace '{workspace}' für diesen Agenten nicht erlaubt.");
|
|
}
|
|
|
|
try
|
|
{
|
|
return action switch
|
|
{
|
|
"read" => await HandleReadAsync(input, rootPath, workspace, context, ct),
|
|
"read_many" => await HandleReadManyAsync(input, rootPath, workspace, context, ct),
|
|
"write" => await HandleWriteAsync(input, rootPath, workspace, context, ct),
|
|
"append" => await HandleAppendAsync(input, rootPath, workspace, context, ct),
|
|
"list" => await HandleListAsync(input, rootPath, workspace, context, ct),
|
|
"delete" => await HandleDeleteAsync(input, rootPath, workspace, context, ct),
|
|
"copy" => await HandleCopyAsync(input, rootPath, workspace, context, ct),
|
|
"stock_add" => await HandleStockAddAsync(input, context, ct),
|
|
_ => ToolResult.Fail($"Unbekannte Aktion: {action}")
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
context.Logger.LogError(ex, "Fehler bei FileRW Aktion {Action}", action);
|
|
return ToolResult.Fail($"Fehler: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// #579: Rechte und Pfad pruefen, bevor ein Aufruf in die Freigabeliste kommt. Dieselben
|
|
/// Pruefungen wie in <see cref="ExecuteAsync"/>, ohne etwas anzufassen.
|
|
/// </summary>
|
|
public string? Preflight(JsonElement input, AgentToolContext context)
|
|
{
|
|
if (input.ValueKind != JsonValueKind.Object
|
|
|| !input.TryGetProperty("action", out var a) || a.ValueKind != JsonValueKind.String)
|
|
return null;
|
|
|
|
var action = a.GetString() ?? "";
|
|
var workspace = (input.TryGetProperty("workspace", out var w) && w.ValueKind == JsonValueKind.String
|
|
? w.GetString() : null) ?? "personal";
|
|
|
|
if (!IsActionAllowed(action, workspace, context))
|
|
return $"Zugriff verweigert: Die Aktion '{action}' ist im Workspace '{workspace}' für diesen Agenten nicht erlaubt.";
|
|
|
|
var rootPath = workspace == "shared" ? context.SharedWorkspacePath : context.WorkspacePath;
|
|
if (action != "delete" || string.IsNullOrWhiteSpace(rootPath))
|
|
return null;
|
|
|
|
try
|
|
{
|
|
rootPath = Path.GetFullPath(rootPath);
|
|
var path = GetAndValidatePath(input, rootPath, workspace, false, context);
|
|
|
|
if (workspace == "shared" && !IsAdminLevel(context) && IsPathProtected(path, rootPath, context))
|
|
return $"🛡️ Geschützter Bereich: '{Path.GetRelativePath(rootPath, path)}' darf nicht gelöscht werden.";
|
|
}
|
|
catch (Exception ex) when (ex is not OperationCanceledException)
|
|
{
|
|
// Dieselbe Pfadpruefung wirft bei der Ausfuehrung - dort wird daraus ein Fehler.
|
|
return $"Zugriff verweigert: {ex.Message}";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private bool IsActionAllowed(string action, string workspace, AgentToolContext context)
|
|
{
|
|
if (workspace == "personal") return true; // Alles erlaubt
|
|
|
|
var level = context.ToolConfig.TryGetValue("sharedAccessLevel", out var lv) ? lv?.ToString() : "Denied";
|
|
|
|
if (level == "Denied") return false;
|
|
|
|
return action switch
|
|
{
|
|
"read" or "read_many" or "list" or "copy" => true,
|
|
"write" or "append" or "stock_add" => level is "ReadWrite" or "Admin",
|
|
"delete" => level is "Admin",
|
|
_ => false
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prüft ob ein Pfad innerhalb eines geschützten Bereichs liegt.
|
|
/// Geschützte Pfade erlauben nur: read, list, append, write (nur neue Dateien), stock_add.
|
|
/// Kein Überschreiben existierender Dateien, kein Löschen.
|
|
/// Admin-Level umgeht den Schutz.
|
|
/// </summary>
|
|
private bool IsPathProtected(string fullPath, string rootPath, AgentToolContext context)
|
|
{
|
|
var protectedPaths = GetProtectedPaths(context);
|
|
if (protectedPaths.Count == 0) return false;
|
|
|
|
var relativePath = Path.GetRelativePath(rootPath, fullPath)
|
|
.Replace('\\', '/').TrimStart('/');
|
|
|
|
// GetProtectedPaths hängt an jeden Eintrag ein "/" an, deshalb greift
|
|
// StartsWith hier auf einer echten Verzeichnisgrenze: "stocks/" trifft
|
|
// "stocks/x.json", aber nicht "stocks-alt/x.json".
|
|
return protectedPaths.Any(pp =>
|
|
relativePath.StartsWith(pp, StringComparison.OrdinalIgnoreCase) ||
|
|
relativePath.Equals(pp.TrimEnd('/'), StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
private bool IsAdminLevel(AgentToolContext context)
|
|
{
|
|
var level = context.ToolConfig.TryGetValue("sharedAccessLevel", out var lv) ? lv?.ToString() : "Denied";
|
|
return level == "Admin";
|
|
}
|
|
|
|
private static List<string> GetProtectedPaths(AgentToolContext context)
|
|
{
|
|
if (!context.ToolConfig.TryGetValue("protectedPaths", out var pp) || pp is null)
|
|
return [];
|
|
|
|
if (pp is JsonElement je && je.ValueKind == JsonValueKind.Array)
|
|
return je.EnumerateArray()
|
|
.Select(e => e.GetString()?.Replace('\\', '/').Trim().TrimEnd('/') + "/")
|
|
.Where(s => !string.IsNullOrEmpty(s))
|
|
.ToList()!;
|
|
|
|
if (pp is string s && !string.IsNullOrWhiteSpace(s))
|
|
return s.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
|
.Select(p => p.Replace('\\', '/').TrimEnd('/') + "/")
|
|
.ToList();
|
|
|
|
return [];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Nachschau nach einem Absturz (#555). Sieht nach, was von einem unterbrochenen
|
|
/// Aufruf übrig ist — verändert nichts.
|
|
///
|
|
/// <para>Wie weit die Auskunft trägt, hängt an der Aktion:</para>
|
|
/// <list type="bullet">
|
|
/// <item><b>write</b> — seit #553 atomar. Entweder die alte oder die neue Fassung,
|
|
/// nie eine halbe. Ein Vergleich mit dem beabsichtigten Inhalt sagt deshalb
|
|
/// zuverlässig, ob der Aufruf durchkam.</item>
|
|
/// <item><b>append</b> — nicht atomar und nicht atomar zu machen. Hier kann wirklich
|
|
/// ein Riss entstehen, und hier lohnt die Nachschau: Steht der beabsichtigte Text
|
|
/// vollständig am Ende, kam der Aufruf durch; steht ein Anfang davon da, riss es
|
|
/// mittendrin.</item>
|
|
/// <item>alles andere — <see cref="ProbeOutcome.Unknown"/>. Lieber schweigen als
|
|
/// raten.</item>
|
|
/// </list>
|
|
/// </summary>
|
|
public async Task<ProbeFinding> ProbeAsync(JsonElement arguments, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var action = arguments.TryGetProperty("action", out var a) ? a.GetString() : null;
|
|
|
|
if (action is not ("write" or "append"))
|
|
return ProbeFinding.Unknown($"Für die Aktion '{action ?? "?"}' gibt es nichts nachzusehen.");
|
|
|
|
var workspace = (arguments.TryGetProperty("workspace", out var w) ? w.GetString() : null) ?? "personal";
|
|
var rootPath = workspace == "shared" ? context.SharedWorkspacePath : context.WorkspacePath;
|
|
|
|
if (string.IsNullOrWhiteSpace(rootPath))
|
|
return ProbeFinding.Unknown("Workspace-Pfad ist nicht bekannt.");
|
|
|
|
var path = GetAndValidatePath(arguments, Path.GetFullPath(rootPath), workspace, true, context);
|
|
var erwartet = arguments.TryGetProperty("content", out var c) ? c.GetString() ?? "" : "";
|
|
|
|
if (!File.Exists(path))
|
|
{
|
|
return new ProbeFinding(ProbeOutcome.NotStarted,
|
|
"Die Datei existiert nicht — geschrieben wurde nichts.");
|
|
}
|
|
|
|
var vorhanden = await File.ReadAllTextAsync(path, ct);
|
|
|
|
if (action == "write")
|
|
{
|
|
return vorhanden == erwartet
|
|
? new ProbeFinding(ProbeOutcome.Complete,
|
|
$"Der Inhalt entspricht genau dem beabsichtigten ({erwartet.Length} Zeichen).")
|
|
: new ProbeFinding(ProbeOutcome.NotStarted,
|
|
"Die Datei trägt noch den alten Inhalt. Seit #553 wird atomar " +
|
|
"geschrieben — eine halbe Fassung kann es nicht geben.");
|
|
}
|
|
|
|
// append
|
|
if (vorhanden.EndsWith(erwartet, StringComparison.Ordinal))
|
|
{
|
|
return new ProbeFinding(ProbeOutcome.Complete,
|
|
$"Der beabsichtigte Text steht vollständig am Ende ({erwartet.Length} Zeichen).");
|
|
}
|
|
|
|
// Wie viel vom Erwarteten steht am Ende? Das ist die Rissstelle.
|
|
for (var laenge = Math.Min(erwartet.Length - 1, vorhanden.Length); laenge > 0; laenge--)
|
|
{
|
|
if (vorhanden.EndsWith(erwartet[..laenge], StringComparison.Ordinal))
|
|
{
|
|
return new ProbeFinding(ProbeOutcome.Partial,
|
|
$"Angehängt wurden {laenge} von {erwartet.Length} Zeichen — " +
|
|
"der Aufruf riss mittendrin ab.");
|
|
}
|
|
}
|
|
|
|
return new ProbeFinding(ProbeOutcome.NotStarted,
|
|
"Vom beabsichtigten Text steht nichts am Ende der Datei.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Nie werfen: Eine gescheiterte Nachschau ist keine Aussage über die Welt,
|
|
// sondern eine über die Nachschau.
|
|
return ProbeFinding.Unknown($"Nachschau nicht möglich: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private string GetAndValidatePath(JsonElement input, string rootPath, string workspace, bool checkExtension, AgentToolContext context)
|
|
{
|
|
var relativePath = input.TryGetProperty("path", out var p) ? p.GetString() : "";
|
|
|
|
// Prüft Ausbruch aus dem Workspace, absolute Pfade und Alternate Data Streams.
|
|
var fullPath = WorkspacePath.Resolve(rootPath, relativePath, workspace);
|
|
|
|
// Extension Check
|
|
if (checkExtension && !Directory.Exists(fullPath))
|
|
{
|
|
var extension = Path.GetExtension(fullPath).ToLowerInvariant();
|
|
|
|
var configKey = workspace == "shared" ? "sharedAllowedExtensions" : "personalAllowedExtensions";
|
|
|
|
var allowedExtensions = context.ToolConfig.TryGetValue(configKey, out var ae) && ae is JsonElement je
|
|
? je.EnumerateArray().Select(x => x.GetString()?.ToLowerInvariant()).ToList()
|
|
: new List<string?> { ".txt", ".json", ".md", ".html", ".js", ".css" }; // Default
|
|
|
|
if (!allowedExtensions.Contains(extension))
|
|
{
|
|
throw new UnauthorizedAccessException($"Dateiendung '{extension}' ist im Workspace '{workspace}' nicht erlaubt.");
|
|
}
|
|
}
|
|
|
|
return fullPath;
|
|
}
|
|
|
|
/// <summary>
|
|
/// #662: Gibt es den Pfad im jeweils anderen Workspace, sagt die Fehlermeldung das.
|
|
///
|
|
/// <para>Anlass aus dem Testbetrieb: Ein guenstiges Modell vergisst regelmaessig
|
|
/// <c>workspace: "shared"</c> und bekommt nur "nicht gefunden" zurueck. Es rät dann
|
|
/// weiter, und jeder Versuch kostet einen Schritt. Der Hinweis kommt nur, wenn der
|
|
/// Agent den anderen Workspace auch benutzen darf - sonst waere er eine Einladung
|
|
/// zu etwas, das ohnehin abgewiesen wird.</para>
|
|
/// </summary>
|
|
private string AndererWorkspaceHinweis(
|
|
string action, string workspace, string? relativePath, AgentToolContext context, bool verzeichnis)
|
|
{
|
|
var anderer = workspace == "shared" ? "personal" : "shared";
|
|
if (!IsActionAllowed(action, anderer, context))
|
|
return "";
|
|
|
|
var root = anderer == "shared" ? context.SharedWorkspacePath : context.WorkspacePath;
|
|
if (string.IsNullOrWhiteSpace(root))
|
|
return "";
|
|
|
|
try
|
|
{
|
|
var pfad = WorkspacePath.Resolve(Path.GetFullPath(root), relativePath, anderer);
|
|
var da = verzeichnis ? Directory.Exists(pfad) : File.Exists(pfad);
|
|
return da
|
|
? $" Der Pfad existiert im {anderer} Workspace — rufe mit workspace: \"{anderer}\" auf."
|
|
: "";
|
|
}
|
|
catch
|
|
{
|
|
return ""; // Ein Pfad, der dort gar nicht auflösbar ist, hilft niemandem.
|
|
}
|
|
}
|
|
|
|
private static string? RelativerPfad(JsonElement input)
|
|
=> input.ValueKind == JsonValueKind.Object && input.TryGetProperty("path", out var p) && p.ValueKind == JsonValueKind.String
|
|
? p.GetString()
|
|
: null;
|
|
|
|
private async Task<ToolResult> HandleReadAsync(JsonElement input, string rootPath, string workspace, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
var path = GetAndValidatePath(input, rootPath, workspace, true, context);
|
|
if (!File.Exists(path))
|
|
{
|
|
return ToolResult.Fail(
|
|
$"Datei nicht gefunden im {workspace} Workspace: {Path.GetRelativePath(rootPath, path)}."
|
|
+ AndererWorkspaceHinweis("read", workspace, RelativerPfad(input), context, verzeichnis: false));
|
|
}
|
|
|
|
var fileLock = GetFileLock(path);
|
|
await fileLock.WaitAsync(ct);
|
|
try
|
|
{
|
|
var content = await File.ReadAllTextAsync(path, ct);
|
|
var relativePath = Path.GetRelativePath(rootPath, path);
|
|
return ToolResult.Ok($"[{workspace}:/{relativePath}]\n{content}");
|
|
}
|
|
finally
|
|
{
|
|
fileLock.Release();
|
|
}
|
|
}
|
|
|
|
/// <summary>Hoechstzahl Dateien je <c>read_many</c>.</summary>
|
|
public const int MaxDateienJeAufruf = 50;
|
|
|
|
/// <summary>Hoechstzahl Zeichen je <c>read_many</c> - danach werden nur noch Namen genannt.</summary>
|
|
public const int MaxZeichenJeAufruf = 200_000;
|
|
|
|
/// <summary>
|
|
/// #663: Mehrere Dateien in einem Aufruf lesen.
|
|
///
|
|
/// <para>Anlass: Der Tagesabschluss im Testbetrieb muss 24 Stundendateien zu einem
|
|
/// Tagesarchiv zusammenfassen. Einzeln gelesen sind das 24 Schritte, und der Lauf
|
|
/// stirbt an der Schrittgrenze, bevor er fertig ist. Die Grenze soll Endlosschleifen
|
|
/// abfangen, nicht ehrliche Arbeit - also liest das Werkzeug jetzt auf einmal.</para>
|
|
///
|
|
/// <para>Entweder <c>paths</c> (ausdrueckliche Liste) oder <c>path</c> als Verzeichnis
|
|
/// mit optionalem <c>pattern</c>. Gedeckelt auf <see cref="MaxDateienJeAufruf"/> Dateien
|
|
/// und <see cref="MaxZeichenJeAufruf"/> Zeichen; was nicht mehr hineinpasst, wird am
|
|
/// Ende namentlich genannt, damit der Agent den Rest gezielt nachholen kann.</para>
|
|
/// </summary>
|
|
private async Task<ToolResult> HandleReadManyAsync(JsonElement input, string rootPath, string workspace, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
var (dateien, fehler) = SammleDateien(input, rootPath, workspace, context);
|
|
if (fehler is not null)
|
|
return ToolResult.Fail(fehler);
|
|
|
|
if (dateien.Count == 0)
|
|
{
|
|
return ToolResult.Fail(
|
|
$"Keine passende Datei im {workspace} Workspace gefunden."
|
|
+ AndererWorkspaceHinweis("read_many", workspace, RelativerPfad(input), context, verzeichnis: true));
|
|
}
|
|
|
|
var uebersprungen = new List<string>();
|
|
var fehlend = new List<string>();
|
|
var text = new StringBuilder();
|
|
var gelesen = 0;
|
|
|
|
foreach (var pfad in dateien.Take(MaxDateienJeAufruf))
|
|
{
|
|
ct.ThrowIfCancellationRequested();
|
|
var relativ = Path.GetRelativePath(rootPath, pfad).Replace('\\', '/');
|
|
|
|
if (!File.Exists(pfad)) { fehlend.Add(relativ); continue; }
|
|
if (text.Length >= MaxZeichenJeAufruf) { uebersprungen.Add(relativ); continue; }
|
|
|
|
var sperre = GetFileLock(pfad);
|
|
await sperre.WaitAsync(ct);
|
|
string inhalt;
|
|
try { inhalt = await File.ReadAllTextAsync(pfad, ct); }
|
|
finally { sperre.Release(); }
|
|
|
|
text.Append($"\n--- {workspace}:/{relativ} ---\n").Append(inhalt.TrimEnd()).Append('\n');
|
|
gelesen++;
|
|
}
|
|
|
|
uebersprungen.AddRange(dateien.Skip(MaxDateienJeAufruf).Select(d => Path.GetRelativePath(rootPath, d).Replace('\\', '/')));
|
|
|
|
var kopf = new StringBuilder($"[{workspace} Workspace] {gelesen} von {dateien.Count} Datei(en) gelesen.");
|
|
if (fehlend.Count > 0)
|
|
kopf.Append($"\nNicht gefunden: {string.Join(", ", fehlend.Take(10))}{(fehlend.Count > 10 ? " …" : "")}");
|
|
if (uebersprungen.Count > 0)
|
|
kopf.Append($"\nNicht mehr gelesen (Grenze {MaxDateienJeAufruf} Dateien / {MaxZeichenJeAufruf} Zeichen), "
|
|
+ $"hole sie mit einem weiteren Aufruf: {string.Join(", ", uebersprungen.Take(10))}{(uebersprungen.Count > 10 ? " …" : "")}");
|
|
|
|
return ToolResult.Ok(kopf.Append('\n').Append(text).ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Die zu lesenden Dateien: entweder aus <c>paths</c> oder aus Verzeichnis und Muster.
|
|
/// Jeder Pfad geht durch dieselbe Pruefung wie ein einzelnes <c>read</c>.
|
|
/// </summary>
|
|
private (List<string> Dateien, string? Fehler) SammleDateien(
|
|
JsonElement input, string rootPath, string workspace, AgentToolContext context)
|
|
{
|
|
try
|
|
{
|
|
if (input.TryGetProperty("paths", out var liste) && liste.ValueKind == JsonValueKind.Array)
|
|
{
|
|
var dateien = new List<string>();
|
|
foreach (var eintrag in liste.EnumerateArray())
|
|
{
|
|
if (eintrag.ValueKind != JsonValueKind.String) continue;
|
|
var relativ = eintrag.GetString();
|
|
if (string.IsNullOrWhiteSpace(relativ)) continue;
|
|
dateien.Add(PruefeLesepfad(relativ, rootPath, workspace, context));
|
|
}
|
|
return (dateien, dateien.Count == 0 ? "'paths' enthaelt keinen Pfad." : null);
|
|
}
|
|
|
|
var verzeichnis = WorkspacePath.Resolve(rootPath, RelativerPfad(input), workspace);
|
|
if (!Directory.Exists(verzeichnis))
|
|
return ([], null); // Die Meldung samt Workspace-Hinweis macht der Aufrufer.
|
|
|
|
var muster = input.TryGetProperty("pattern", out var m) && m.ValueKind == JsonValueKind.String
|
|
? m.GetString()
|
|
: null;
|
|
if (MusterFehler(muster) is { } musterFehler)
|
|
return ([], musterFehler);
|
|
|
|
var gefunden = Directory.GetFiles(verzeichnis, string.IsNullOrWhiteSpace(muster) ? "*" : muster)
|
|
.OrderBy(f => f, StringComparer.Ordinal)
|
|
.ToList();
|
|
|
|
// Dieselbe Endungspruefung wie beim einzelnen Lesen - was dort verboten ist,
|
|
// kommt auch hier nicht durch, nur ohne den Lauf abzubrechen.
|
|
return (gefunden.Where(f => IstLesbar(f, workspace, context)).ToList(), null);
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
return ([], $"Zugriff verweigert: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private string PruefeLesepfad(string relativePath, string rootPath, string workspace, AgentToolContext context)
|
|
{
|
|
var json = JsonSerializer.SerializeToElement(new { action = "read", path = relativePath });
|
|
return GetAndValidatePath(json, rootPath, workspace, true, context);
|
|
}
|
|
|
|
private bool IstLesbar(string vollerPfad, string workspace, AgentToolContext context)
|
|
{
|
|
try
|
|
{
|
|
var json = JsonSerializer.SerializeToElement(new { action = "read", path = Path.GetFileName(vollerPfad) });
|
|
GetAndValidatePath(json, Path.GetDirectoryName(vollerPfad)!, workspace, true, context);
|
|
return true;
|
|
}
|
|
catch (UnauthorizedAccessException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async Task<ToolResult> HandleWriteAsync(JsonElement input, string rootPath, string workspace, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
var path = GetAndValidatePath(input, rootPath, workspace, true, context);
|
|
var content = input.TryGetProperty("content", out var c) ? c.GetString() ?? "" : "";
|
|
|
|
var fileLock = GetFileLock(path);
|
|
await fileLock.WaitAsync(ct);
|
|
try
|
|
{
|
|
// Schutz: In geschützten Bereichen darf nur geschrieben werden wenn die Datei NICHT existiert
|
|
if (workspace == "shared" && !IsAdminLevel(context) && File.Exists(path) && IsPathProtected(path, rootPath, context))
|
|
{
|
|
var relPath = Path.GetRelativePath(rootPath, path);
|
|
return ToolResult.Fail(
|
|
$"🛡️ Geschützter Bereich: Die Datei '{relPath}' existiert bereits und darf nicht überschrieben werden. " +
|
|
$"Verwende 'append' um Daten hinzuzufügen, oder erstelle eine neue Datei mit anderem Namen. " +
|
|
$"Tipp: Nutze 'stock_add' für strukturierte Datenpunkte.");
|
|
}
|
|
|
|
var dir = Path.GetDirectoryName(path);
|
|
if (dir != null && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
|
|
|
|
// Atomar schreiben (#553): erst in eine Nebendatei, dann ersetzen. Ein Absturz
|
|
// mitten im Vorgang hinterlässt damit entweder die alte oder die neue Fassung,
|
|
// nie eine halbe. Zuvor stand hier File.WriteAllTextAsync — die Anwendung
|
|
// schützte also ihre eigenen Konfigurationsdateien, die Arbeitsergebnisse der
|
|
// Agenten aber nicht.
|
|
await AtomicFile.WriteAllTextAsync(path, content, new UTF8Encoding(false), ct);
|
|
return ToolResult.Ok($"[{workspace} Workspace] Datei erfolgreich geschrieben: {workspace}:/{Path.GetRelativePath(rootPath, path)}");
|
|
}
|
|
finally
|
|
{
|
|
fileLock.Release();
|
|
}
|
|
}
|
|
|
|
private async Task<ToolResult> HandleAppendAsync(JsonElement input, string rootPath, string workspace, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
var path = GetAndValidatePath(input, rootPath, workspace, true, context);
|
|
var content = input.TryGetProperty("content", out var c) ? c.GetString() ?? "" : "";
|
|
|
|
var fileLock = GetFileLock(path);
|
|
await fileLock.WaitAsync(ct);
|
|
try
|
|
{
|
|
// Bewusst NICHT atomar (#553): Anhängen ließe sich nur atomar machen, indem
|
|
// die ganze Datei neu geschrieben wird — bei einer großen Protokolldatei wäre
|
|
// das der falsche Handel. Hier bleibt ein Riss möglich; genau dafür gibt es
|
|
// die Zustandsprüfung in #555.
|
|
// #575: Neu entstehende Dateien gehoeren nur dem Besitzer (0600).
|
|
await using (var stream = ClawdDotNet.Core.Storage.OwnerOnlyFile.OpenAppend(path))
|
|
await using (var writer = new StreamWriter(stream, new UTF8Encoding(false)))
|
|
await writer.WriteAsync(content.AsMemory(), ct);
|
|
return ToolResult.Ok($"[{workspace} Workspace] Inhalt erfolgreich angehängt an: {workspace}:/{Path.GetRelativePath(rootPath, path)}");
|
|
}
|
|
finally
|
|
{
|
|
fileLock.Release();
|
|
}
|
|
}
|
|
|
|
/// <summary>Wie viele Eintraege ein <c>list</c> ohne ausdrueckliches <c>limit</c> zeigt.</summary>
|
|
public const int StandardEintraege = 50;
|
|
|
|
/// <summary>Obergrenze fuer <c>limit</c> - darueber wird die Antwort zur Heuhaufensuche.</summary>
|
|
public const int MaxEintraege = 500;
|
|
|
|
/// <summary>
|
|
/// #673: Verzeichnis auflisten - sortiert, gedeckelt, kompakt.
|
|
///
|
|
/// <para>Anlass: Die Betriebskontrolle im Testbetrieb meldete Stundendateien als fehlend,
|
|
/// die es gab. Das Verzeichnis lieferte 105 Eintraege in der Reihenfolge des Dateisystems,
|
|
/// vierzeilig als eingeruecktes JSON. Darin zwei bestimmte Namen zu finden ist Sucharbeit,
|
|
/// und ein guenstiges Modell erledigt sie falsch.</para>
|
|
///
|
|
/// <para>Also: neueste zuerst (umstellbar ueber <c>sort</c> und <c>order</c>), <c>pattern</c>
|
|
/// zum Eingrenzen, eine Zeile je Eintrag - und der Kopf sagt, wie viele Eintraege es
|
|
/// insgesamt gibt. Eine gedeckelte Liste, die ihre Deckelung verschweigt, waere die
|
|
/// schlimmere Falle.</para>
|
|
/// </summary>
|
|
private async Task<ToolResult> HandleListAsync(JsonElement input, string rootPath, string workspace, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
var path = GetAndValidatePath(input, rootPath, workspace, false, context);
|
|
if (!Directory.Exists(path))
|
|
{
|
|
return ToolResult.Fail(
|
|
$"Verzeichnis nicht gefunden im {workspace} Workspace: {Path.GetRelativePath(rootPath, path)}."
|
|
+ AndererWorkspaceHinweis("list", workspace, RelativerPfad(input), context, verzeichnis: true));
|
|
}
|
|
|
|
var muster = GetOptionalString(input, "pattern");
|
|
if (MusterFehler(muster) is { } musterFehler)
|
|
return ToolResult.Fail(musterFehler);
|
|
|
|
List<string> pfade;
|
|
try
|
|
{
|
|
pfade = Directory.GetFileSystemEntries(path, string.IsNullOrWhiteSpace(muster) ? "*" : muster).ToList();
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
return ToolResult.Fail($"Ungueltiges Muster '{muster}': {ex.Message}");
|
|
}
|
|
|
|
var eintraege = pfade
|
|
.Select(e => new Verzeichniseintrag(
|
|
Path.GetFileName(e),
|
|
Directory.Exists(e),
|
|
Directory.Exists(e) ? 0 : new FileInfo(e).Length,
|
|
File.GetLastWriteTime(e)))
|
|
.ToList();
|
|
|
|
var nachName = string.Equals(GetOptionalString(input, "sort"), "name", StringComparison.OrdinalIgnoreCase);
|
|
var absteigend = GetOptionalString(input, "order") is { } o
|
|
? string.Equals(o, "desc", StringComparison.OrdinalIgnoreCase)
|
|
: !nachName; // neueste zuerst, Namen aufsteigend
|
|
|
|
IEnumerable<Verzeichniseintrag> sortiert = nachName
|
|
? (absteigend
|
|
? eintraege.OrderByDescending(e => e.Name, StringComparer.OrdinalIgnoreCase)
|
|
: eintraege.OrderBy(e => e.Name, StringComparer.OrdinalIgnoreCase))
|
|
: (absteigend
|
|
? eintraege.OrderByDescending(e => e.Geaendert)
|
|
: eintraege.OrderBy(e => e.Geaendert));
|
|
|
|
var grenze = Math.Clamp(GetInt(input, "limit") ?? StandardEintraege, 1, MaxEintraege);
|
|
var gezeigt = sortiert.Take(grenze).ToList();
|
|
|
|
var relativeDirPath = Path.GetRelativePath(rootPath, path);
|
|
var ordnung = nachName
|
|
? (absteigend ? "Name absteigend" : "Name aufsteigend")
|
|
: (absteigend ? "neueste zuerst" : "aelteste zuerst");
|
|
|
|
var text = new StringBuilder();
|
|
text.Append($"[{workspace} Workspace] Verzeichnis: {workspace}:/{(relativeDirPath == "." ? "" : relativeDirPath)}");
|
|
text.Append($" - {gezeigt.Count} von {eintraege.Count} Eintraegen, {ordnung}.");
|
|
if (!string.IsNullOrWhiteSpace(muster))
|
|
text.Append($" Muster: {muster}.");
|
|
if (eintraege.Count > gezeigt.Count)
|
|
text.Append($"\n{eintraege.Count - gezeigt.Count} weitere nicht gezeigt - grenze mit 'pattern' ein oder setze 'limit' (max {MaxEintraege}).");
|
|
text.Append("\nGeaendert Groesse Name\n");
|
|
|
|
foreach (var e in gezeigt)
|
|
{
|
|
var groesse = e.IstVerzeichnis ? "<DIR>" : e.Groesse.ToString();
|
|
text.Append($"{e.Geaendert:yyyy-MM-dd HH:mm} {groesse,9} {e.Name}{(e.IstVerzeichnis ? "/" : "")}\n");
|
|
}
|
|
|
|
return await Task.FromResult(ToolResult.Ok(text.ToString().TrimEnd()));
|
|
}
|
|
|
|
private readonly record struct Verzeichniseintrag(string Name, bool IstVerzeichnis, long Groesse, DateTime Geaendert);
|
|
|
|
/// <summary>
|
|
/// Ein Muster ist ein Dateiname mit Platzhaltern, kein Pfad. <c>Directory.GetFiles</c>
|
|
/// wertet Trennzeichen im Muster aus — <c>pattern: "../*"</c> listet damit das
|
|
/// Elternverzeichnis, an der Pfadpruefung vorbei. Deshalb hier und nicht erst dort.
|
|
/// </summary>
|
|
private static string? MusterFehler(string? muster)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(muster)) return null;
|
|
|
|
if (muster.Contains('/') || muster.Contains('\\') || muster.Contains(".."))
|
|
return $"Ungueltiges Muster '{muster}': ein Muster ist ein Dateiname mit Platzhaltern "
|
|
+ "(z.B. 'berlin_2026-09-21_*.txt'), kein Pfad. Das Verzeichnis gehoert in 'path'.";
|
|
|
|
return null;
|
|
}
|
|
|
|
private static string? GetOptionalString(JsonElement input, string name)
|
|
=> input.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() : null;
|
|
|
|
private static int? GetInt(JsonElement input, string name)
|
|
=> input.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.Number && v.TryGetInt32(out var i) ? i : null;
|
|
|
|
private async Task<ToolResult> HandleDeleteAsync(JsonElement input, string rootPath, string workspace, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
var path = GetAndValidatePath(input, rootPath, workspace, false, context);
|
|
|
|
// Schutz: In geschützten Bereichen ist Löschen komplett verboten (außer Admin)
|
|
if (workspace == "shared" && !IsAdminLevel(context) && IsPathProtected(path, rootPath, context))
|
|
{
|
|
var relPath = Path.GetRelativePath(rootPath, path);
|
|
return ToolResult.Fail(
|
|
$"🛡️ Geschützter Bereich: '{relPath}' liegt in einem geschützten Verzeichnis und darf nicht gelöscht werden. " +
|
|
$"Daten in geschützten Bereichen sind append-only — sie können nur ergänzt, nicht entfernt werden.");
|
|
}
|
|
|
|
var fileLock = GetFileLock(path);
|
|
await fileLock.WaitAsync(ct);
|
|
try
|
|
{
|
|
if (File.Exists(path))
|
|
{
|
|
File.Delete(path);
|
|
return ToolResult.Ok($"[{workspace} Workspace] Datei gelöscht: {workspace}:/{Path.GetRelativePath(rootPath, path)}");
|
|
}
|
|
else if (Directory.Exists(path))
|
|
{
|
|
Directory.Delete(path, true);
|
|
return ToolResult.Ok($"[{workspace} Workspace] Verzeichnis gelöscht: {workspace}:/{Path.GetRelativePath(rootPath, path)}");
|
|
}
|
|
|
|
return ToolResult.Fail($"Datei oder Verzeichnis nicht gefunden im {workspace} Workspace.");
|
|
}
|
|
finally
|
|
{
|
|
fileLock.Release();
|
|
}
|
|
}
|
|
|
|
private async Task<ToolResult> HandleCopyAsync(JsonElement input, string sourceRootPath, string sourceWorkspace, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
var sourcePath = GetAndValidatePath(input, sourceRootPath, sourceWorkspace, false, context);
|
|
|
|
var destWorkspace = (input.TryGetProperty("destinationWorkspace", out var dw) ? dw.GetString() : null) ?? sourceWorkspace;
|
|
var destRelPath = input.TryGetProperty("destinationPath", out var dp) ? dp.GetString() : null;
|
|
|
|
if (string.IsNullOrWhiteSpace(destRelPath))
|
|
return ToolResult.Fail("'destinationPath' ist für die copy-Aktion erforderlich.");
|
|
|
|
var destRootPath = destWorkspace == "shared" ? context.SharedWorkspacePath : context.WorkspacePath;
|
|
if (string.IsNullOrWhiteSpace(destRootPath))
|
|
return ToolResult.Fail($"Konfigurationsfehler: '{(destWorkspace == "shared" ? "SharedWorkspace" : "PersonalWorkspace")}' ist nicht definiert.");
|
|
|
|
destRootPath = Path.GetFullPath(destRootPath);
|
|
|
|
if (!IsActionAllowed("write", destWorkspace, context))
|
|
return ToolResult.Fail($"Zugriff verweigert: Schreiben im Workspace '{destWorkspace}' ist für diesen Agenten nicht erlaubt.");
|
|
|
|
var destInput = JsonDocument.Parse(JsonSerializer.Serialize(new { path = destRelPath })).RootElement;
|
|
var destPath = GetAndValidatePath(destInput, destRootPath, destWorkspace, false, context);
|
|
|
|
if (File.Exists(sourcePath))
|
|
{
|
|
// Schutz: Kein Überschreiben in geschützten Bereichen per Copy
|
|
if (destWorkspace == "shared" && !IsAdminLevel(context) && File.Exists(destPath) && IsPathProtected(destPath, destRootPath, context))
|
|
{
|
|
var relPath = Path.GetRelativePath(destRootPath, destPath);
|
|
return ToolResult.Fail(
|
|
$"🛡️ Geschützter Bereich: Die Zieldatei '{relPath}' existiert bereits und darf nicht überschrieben werden.");
|
|
}
|
|
|
|
// Deadlock-sicheres Locking: Immer in alphabetischer Reihenfolge locken
|
|
var srcNorm = NormalizePath(sourcePath);
|
|
var dstNorm = NormalizePath(destPath);
|
|
var first = string.Compare(srcNorm, dstNorm, StringComparison.Ordinal) <= 0
|
|
? GetFileLock(sourcePath) : GetFileLock(destPath);
|
|
var second = string.Compare(srcNorm, dstNorm, StringComparison.Ordinal) <= 0
|
|
? GetFileLock(destPath) : GetFileLock(sourcePath);
|
|
|
|
await first.WaitAsync(ct);
|
|
try
|
|
{
|
|
await second.WaitAsync(ct);
|
|
try
|
|
{
|
|
var destDir = Path.GetDirectoryName(destPath);
|
|
if (destDir != null && !Directory.Exists(destDir)) Directory.CreateDirectory(destDir);
|
|
|
|
await Task.Run(() => File.Copy(sourcePath, destPath, overwrite: !IsPathProtected(destPath, destRootPath, context)), ct);
|
|
|
|
var srcLabel = $"{sourceWorkspace}:/{Path.GetRelativePath(sourceRootPath, sourcePath)}";
|
|
var dstLabel = $"{destWorkspace}:/{Path.GetRelativePath(destRootPath, destPath)}";
|
|
return ToolResult.Ok($"Datei kopiert: {srcLabel} → {dstLabel}");
|
|
}
|
|
finally
|
|
{
|
|
second.Release();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
first.Release();
|
|
}
|
|
}
|
|
else if (Directory.Exists(sourcePath))
|
|
{
|
|
await Task.Run(() => CopyDirectory(sourcePath, destPath), ct);
|
|
|
|
var srcLabel = $"{sourceWorkspace}:/{Path.GetRelativePath(sourceRootPath, sourcePath)}";
|
|
var dstLabel = $"{destWorkspace}:/{Path.GetRelativePath(destRootPath, destPath)}";
|
|
return ToolResult.Ok($"Verzeichnis kopiert: {srcLabel} → {dstLabel}");
|
|
}
|
|
|
|
return ToolResult.Fail("Quelldatei oder -verzeichnis nicht gefunden.");
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════
|
|
// STOCK DATABASE (Append-Only Wissensdatenbank)
|
|
// ═══════════════════════════════════════════════════
|
|
|
|
private static readonly JsonSerializerOptions StockJsonOpts = new()
|
|
{
|
|
WriteIndented = true,
|
|
PropertyNameCaseInsensitive = true,
|
|
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
|
};
|
|
|
|
private async Task<ToolResult> HandleStockAddAsync(JsonElement input, AgentToolContext context, CancellationToken ct)
|
|
{
|
|
// Pflichtfelder
|
|
var ticker = (input.TryGetProperty("ticker", out var t) ? t.GetString() : null)?.Trim().ToUpperInvariant();
|
|
if (string.IsNullOrWhiteSpace(ticker))
|
|
return ToolResult.Fail("'ticker' ist ein Pflichtfeld für stock_add (z.B. 'NVDA', 'TSLA').");
|
|
|
|
var category = (input.TryGetProperty("category", out var cat) ? cat.GetString() : null) ?? "other";
|
|
var source = (input.TryGetProperty("source", out var src) ? src.GetString() : null) ?? "unknown";
|
|
var title = (input.TryGetProperty("title", out var ttl) ? ttl.GetString() : null) ?? "";
|
|
var content = input.TryGetProperty("content", out var cnt) ? cnt.GetString() : null;
|
|
var data = input.TryGetProperty("data", out var d) ? d : (JsonElement?)null;
|
|
|
|
if (string.IsNullOrWhiteSpace(title) && string.IsNullOrWhiteSpace(content) && data is null)
|
|
return ToolResult.Fail("Mindestens 'title', 'content' oder 'data' muss angegeben werden.");
|
|
|
|
// SharedWorkspace ist Pflicht für stocks
|
|
var sharedRoot = context.SharedWorkspacePath;
|
|
if (string.IsNullOrWhiteSpace(sharedRoot))
|
|
return ToolResult.Fail("SharedWorkspace ist nicht konfiguriert.");
|
|
|
|
sharedRoot = Path.GetFullPath(sharedRoot);
|
|
|
|
// Zugriffsprüfung
|
|
if (!IsActionAllowed("stock_add", "shared", context))
|
|
return ToolResult.Fail("Zugriff verweigert: Schreiben im SharedWorkspace nicht erlaubt.");
|
|
|
|
// Sanitize Ticker für Verzeichnisname
|
|
var safeTicker = SanitizeFileName(ticker);
|
|
var stockDir = Path.Combine(sharedRoot, "stocks", safeTicker);
|
|
Directory.CreateDirectory(stockDir);
|
|
|
|
// Timestamp-basierten Dateinamen generieren
|
|
var now = DateTime.UtcNow;
|
|
var timestamp = now.ToString("yyyyMMdd_HHmmss");
|
|
var safeSource = SanitizeFileName(source);
|
|
var fileName = $"{timestamp}_{category}_{safeSource}.json";
|
|
var filePath = Path.Combine(stockDir, fileName);
|
|
|
|
// Kollisionsvermeidung
|
|
var counter = 1;
|
|
while (File.Exists(filePath))
|
|
{
|
|
fileName = $"{timestamp}_{category}_{safeSource}_{counter}.json";
|
|
filePath = Path.Combine(stockDir, fileName);
|
|
counter++;
|
|
}
|
|
|
|
// Datenpunkt erstellen
|
|
var entry = new Dictionary<string, object?>
|
|
{
|
|
["id"] = $"{safeTicker}_{timestamp}_{category}_{safeSource}",
|
|
["ticker"] = ticker,
|
|
["category"] = category,
|
|
["source"] = source,
|
|
["title"] = title,
|
|
["timestamp"] = now.ToString("o"),
|
|
["agent"] = context.AgentId
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(content))
|
|
entry["content"] = content;
|
|
|
|
if (data.HasValue)
|
|
entry["data"] = data.Value;
|
|
|
|
// Datenpunkt speichern
|
|
var json = JsonSerializer.Serialize(entry, StockJsonOpts);
|
|
await ClawdDotNet.Core.Storage.AtomicFile.WriteAllTextAsync(filePath, json, new UTF8Encoding(false), ct);
|
|
|
|
// _index.json aktualisieren (thread-safe via per-file SemaphoreSlim, append-only)
|
|
var indexPath = Path.Combine(stockDir, "_index.json");
|
|
var indexEntry = new Dictionary<string, object?>
|
|
{
|
|
["file"] = fileName,
|
|
["category"] = category,
|
|
["source"] = source,
|
|
["title"] = title,
|
|
["timestamp"] = now.ToString("o"),
|
|
["agent"] = context.AgentId
|
|
};
|
|
|
|
int indexCount;
|
|
|
|
var indexLock = GetFileLock(indexPath);
|
|
await indexLock.WaitAsync(ct);
|
|
try
|
|
{
|
|
List<Dictionary<string, object?>> index;
|
|
if (File.Exists(indexPath))
|
|
{
|
|
try
|
|
{
|
|
var existing = await File.ReadAllTextAsync(indexPath, ct);
|
|
index = JsonSerializer.Deserialize<List<Dictionary<string, object?>>>(existing, StockJsonOpts)
|
|
?? [];
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
// Korrupter Index: Backup + Neustart
|
|
var backup = indexPath + $".bak_{now:yyyyMMdd_HHmmss}";
|
|
try { File.Copy(indexPath, backup, overwrite: true); } catch { /* best effort */ }
|
|
index = [];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
index = [];
|
|
}
|
|
|
|
index.Add(indexEntry);
|
|
// Atomar: Der Index ist die einzige Übersicht über die Datenpunkte —
|
|
// eine halb geschriebene Datei wäre nicht wiederherstellbar.
|
|
await ClawdDotNet.Core.Storage.AtomicFile.WriteAllTextAsync(
|
|
indexPath, JsonSerializer.Serialize(index, StockJsonOpts), ct: ct);
|
|
|
|
// Den Stand hier festhalten, solange die Sperre noch gilt (B9). Vorher
|
|
// wurde die Datei nach dem Freigeben ein zweites Mal gelesen und geparst:
|
|
// ein paralleler stock_add konnte dazwischenschreiben, und die gemeldete
|
|
// Zahl gehörte dann zu einem anderen Aufruf als die gemeldete Datei.
|
|
// Innerhalb der Sperre ist der Zähler ohnehin bekannt.
|
|
indexCount = index.Count;
|
|
}
|
|
finally
|
|
{
|
|
indexLock.Release();
|
|
}
|
|
|
|
var relPath = $"shared:/stocks/{safeTicker}/{fileName}";
|
|
return ToolResult.Ok(
|
|
$"✅ Datenpunkt hinzugefügt: {relPath}\n" +
|
|
$"Ticker: {ticker} | Kategorie: {category} | Quelle: {source}\n" +
|
|
$"Titel: {title}\n" +
|
|
$"Index: {indexCount} Einträge für {ticker}");
|
|
}
|
|
|
|
private static string SanitizeFileName(string name)
|
|
{
|
|
var sanitized = name.Trim();
|
|
foreach (var c in Path.GetInvalidFileNameChars())
|
|
sanitized = sanitized.Replace(c, '_');
|
|
return sanitized.Replace(' ', '_');
|
|
}
|
|
|
|
private static void CopyDirectory(string sourceDir, string destinationDir)
|
|
{
|
|
Directory.CreateDirectory(destinationDir);
|
|
|
|
foreach (var file in Directory.GetFiles(sourceDir))
|
|
File.Copy(file, Path.Combine(destinationDir, Path.GetFileName(file)), overwrite: true);
|
|
|
|
foreach (var dir in Directory.GetDirectories(sourceDir))
|
|
CopyDirectory(dir, Path.Combine(destinationDir, Path.GetFileName(dir)));
|
|
}
|
|
}
|