summaryrefslogtreecommitdiff
path: root/src/extension_loader.zig
blob: 2a35d15bcf5938015c88677a81825696597ef418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Extension discovery: walk well-known directories, locate Lua extensions,
//! and load each one into a long-lived `LuaRuntime`.
//!
//! Search order (later entries shadow earlier ones by extension *name*):
//!   1. `${XDG_CONFIG_HOME:-$HOME/.config}/panto/extensions/`  ("user")
//!   2. `./.panto/extensions/`                                  ("project")
//!
//! Layout per directory:
//!   - `<name>.lua`        -- single-file extension; the extension name is
//!                            the basename without the `.lua` suffix.
//!   - `<name>/init.lua`   -- directory extension; the extension name is
//!                            the directory name. The directory is added
//!                            to the extension's `package.path` so it can
//!                            `require` sibling Lua files.
//!
//! Conflict rules:
//!   - Within one directory, two entries with the same extension name
//!     are an error.
//!   - Project shadows user by extension name (debug-logged, not an error).
//!   - Tool-name collisions *between* loaded extensions are an error: a
//!     tool name is a contract the LLM relies on.
//!
//! Symlinks: followed normally. Dotfiles: skipped.

const std = @import("std");
const panto = @import("panto");
const lua_runtime = @import("lua_runtime.zig");

const Allocator = std.mem.Allocator;
const Io = std.Io;
const LuaRuntime = lua_runtime.LuaRuntime;

/// A discovered extension before loading. Owns its strings.
const Found = struct {
    /// Logical name (basename without `.lua`, or directory name).
    name: []u8,
    /// Absolute path to the Lua script to execute.
    script_path: []u8,
    /// For directory-style extensions, the directory containing the script.
    package_root: ?[]u8,
    /// Which search-path source this came from.
    source: Source,

    pub fn deinit(self: *Found, allocator: Allocator) void {
        allocator.free(self.name);
        allocator.free(self.script_path);
        if (self.package_root) |p| allocator.free(p);
    }
};

pub const Source = enum {
    user,
    project,

    pub fn label(self: Source) []const u8 {
        return switch (self) {
            .user => "user",
            .project => "project",
        };
    }
};

/// Discover and load every extension found in the standard paths into
/// `runtime`. Returns the number of *tools* (not extensions) declared.
///
/// `environ_map` is consulted for `HOME` and `XDG_CONFIG_HOME`. The
/// project directory is always `cwd()/.panto/extensions`.
pub fn discoverAndLoad(
    allocator: Allocator,
    io: Io,
    environ_map: *const std.process.Environ.Map,
    runtime: *LuaRuntime,
) !usize {
    const user_dir = try userExtensionsDir(allocator, environ_map);
    defer if (user_dir) |d| allocator.free(d);

    const project_dir = try projectExtensionsDir(allocator, io);
    defer allocator.free(project_dir);

    return loadFromDirs(allocator, io, runtime, user_dir, project_dir);
}

/// Lower-level entry point: load from explicit user/project paths.
/// Either path may be null; missing directories are silently skipped.
pub fn loadFromDirs(
    allocator: Allocator,
    io: Io,
    runtime: *LuaRuntime,
    user_dir: ?[]const u8,
    project_dir: ?[]const u8,
) !usize {
    var found: std.array_list.Managed(Found) = .init(allocator);
    defer {
        for (found.items) |*f| f.deinit(allocator);
        found.deinit();
    }

    if (user_dir) |d| try scanDir(allocator, io, d, .user, &found);
    if (project_dir) |d| try scanDir(allocator, io, d, .project, &found);

    try applyShadowing(allocator, &found);

    const before = runtime.toolCount();
    for (found.items) |f| {
        runtime.loadExtension(f.script_path, f.package_root) catch |err| {
            if (@import("builtin").is_test) {
                std.log.warn(
                    "extension '{s}' ({s}: {s}) failed to load: {t}",
                    .{ f.name, f.source.label(), f.script_path, err },
                );
            } else {
                std.log.err(
                    "extension '{s}' ({s}: {s}) failed to load: {t}",
                    .{ f.name, f.source.label(), f.script_path, err },
                );
            }
            return err;
        };
        std.log.debug(
            "extension: loaded '{s}' ({s})",
            .{ f.name, f.source.label() },
        );
    }
    return runtime.toolCount() - before;
}

// ---------------------------------------------------------------------------
// Path resolution
// ---------------------------------------------------------------------------

fn userExtensionsDir(
    allocator: Allocator,
    environ_map: *const std.process.Environ.Map,
) !?[]u8 {
    if (environ_map.get("XDG_CONFIG_HOME")) |xdg| {
        return try std.fs.path.join(allocator, &.{ xdg, "panto", "extensions" });
    }
    if (environ_map.get("HOME")) |home| {
        return try std.fs.path.join(allocator, &.{ home, ".config", "panto", "extensions" });
    }
    return null;
}

fn projectExtensionsDir(allocator: Allocator, io: Io) ![]u8 {
    const cwd = try std.process.currentPathAlloc(io, allocator);
    defer allocator.free(cwd);
    return try std.fs.path.join(allocator, &.{ cwd, ".panto", "extensions" });
}

// ---------------------------------------------------------------------------
// Directory scanning
// ---------------------------------------------------------------------------

fn scanDir(
    allocator: Allocator,
    io: Io,
    dir_path: []const u8,
    source: Source,
    out: *std.array_list.Managed(Found),
) !void {
    var dir = Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }) catch |err| switch (err) {
        error.FileNotFound, error.NotDir => return,
        else => |e| return e,
    };
    defer dir.close(io);

    var local_names: std.StringHashMap(void) = .init(allocator);
    defer {
        var it = local_names.keyIterator();
        while (it.next()) |k| allocator.free(k.*);
        local_names.deinit();
    }

    var iter = dir.iterate();
    while (try iter.next(io)) |entry| {
        if (entry.name.len == 0 or entry.name[0] == '.') continue;

        const maybe_found: ?Found = switch (entry.kind) {
            .file, .sym_link => try classifyFile(allocator, dir_path, entry.name, source),
            .directory => try classifyDirectory(allocator, io, dir, dir_path, entry.name, source),
            else => null,
        };
        const f = maybe_found orelse continue;

        const gop = try local_names.getOrPut(f.name);
        if (gop.found_existing) {
            if (@import("builtin").is_test) {
                std.log.warn(
                    "extension name '{s}' is provided by multiple entries in {s}",
                    .{ f.name, dir_path },
                );
            } else {
                std.log.err(
                    "extension name '{s}' is provided by multiple entries in {s}",
                    .{ f.name, dir_path },
                );
            }
            var dup = f;
            dup.deinit(allocator);
            return error.DuplicateExtensionInDirectory;
        }
        gop.key_ptr.* = try allocator.dupe(u8, f.name);

        try out.append(f);
    }
}

fn classifyFile(
    allocator: Allocator,
    dir_path: []const u8,
    entry_name: []const u8,
    source: Source,
) !?Found {
    if (!std.mem.endsWith(u8, entry_name, ".lua")) return null;
    const base = entry_name[0 .. entry_name.len - ".lua".len];
    if (base.len == 0) return null;

    const script_path = try std.fs.path.join(allocator, &.{ dir_path, entry_name });
    errdefer allocator.free(script_path);
    const name = try allocator.dupe(u8, base);
    errdefer allocator.free(name);

    return Found{
        .name = name,
        .script_path = script_path,
        .package_root = null,
        .source = source,
    };
}

fn classifyDirectory(
    allocator: Allocator,
    io: Io,
    parent: Io.Dir,
    dir_path: []const u8,
    entry_name: []const u8,
    source: Source,
) !?Found {
    var sub = parent.openDir(io, entry_name, .{}) catch return null;
    defer sub.close(io);

    sub.access(io, "init.lua", .{}) catch |err| switch (err) {
        error.FileNotFound => return null,
        else => return null,
    };

    const package_root = try std.fs.path.join(allocator, &.{ dir_path, entry_name });
    errdefer allocator.free(package_root);
    const script_path = try std.fs.path.join(allocator, &.{ package_root, "init.lua" });
    errdefer allocator.free(script_path);
    const name = try allocator.dupe(u8, entry_name);
    errdefer allocator.free(name);

    return Found{
        .name = name,
        .script_path = script_path,
        .package_root = package_root,
        .source = source,
    };
}

// ---------------------------------------------------------------------------
// Shadowing
// ---------------------------------------------------------------------------

fn applyShadowing(allocator: Allocator, list: *std.array_list.Managed(Found)) !void {
    var latest: std.StringHashMap(usize) = .init(allocator);

    for (list.items, 0..) |f, i| {
        try latest.put(f.name, i);
    }

    var keep: std.array_list.Managed(Found) = .init(allocator);
    var drop: std.array_list.Managed(Found) = .init(allocator);
    errdefer {
        latest.deinit();
        for (keep.items) |*f| f.deinit(allocator);
        keep.deinit();
        for (drop.items) |*f| f.deinit(allocator);
        drop.deinit();
    }
    try keep.ensureTotalCapacity(list.items.len);
    try drop.ensureTotalCapacity(list.items.len);

    for (list.items, 0..) |f, i| {
        const winner = latest.get(f.name).?;
        if (winner == i) {
            keep.appendAssumeCapacity(f);
        } else {
            std.log.debug(
                "extension: '{s}' from {s} shadowed by {s}",
                .{ f.name, f.source.label(), list.items[winner].source.label() },
            );
            drop.appendAssumeCapacity(f);
        }
    }

    latest.deinit();
    for (drop.items) |*f| f.deinit(allocator);
    drop.deinit();

    list.clearRetainingCapacity();
    list.appendSlice(keep.items) catch unreachable;
    keep.deinit();
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

const testing = std.testing;

fn writeFile(dir: Io.Dir, sub_path: []const u8, content: []const u8) !void {
    try dir.writeFile(testing.io, .{ .sub_path = sub_path, .data = content });
}

fn makeDir(dir: Io.Dir, sub_path: []const u8) !void {
    try dir.createDirPath(testing.io, sub_path);
}

test "scanDir picks up single-file and directory-style extensions" {
    var tmp = testing.tmpDir(.{ .iterate = true });
    defer tmp.cleanup();

    try makeDir(tmp.dir, "ext_root");
    try makeDir(tmp.dir, "ext_root/beta");
    try writeFile(tmp.dir, "ext_root/alpha.lua", "-- alpha\n");
    try writeFile(tmp.dir, "ext_root/beta/init.lua", "-- beta init\n");
    try writeFile(tmp.dir, "ext_root/beta/helper.lua", "-- helper\n");
    try writeFile(tmp.dir, "ext_root/.ignored.lua", "-- hidden\n");
    try writeFile(tmp.dir, "ext_root/readme.txt", "noise\n");

    var path_buf: [std.fs.max_path_bytes]u8 = undefined;
    const ext_root_len = try tmp.dir.realPathFile(testing.io, "ext_root", &path_buf);
    const ext_root = path_buf[0..ext_root_len];

    var list: std.array_list.Managed(Found) = .init(testing.allocator);
    defer {
        for (list.items) |*f| f.deinit(testing.allocator);
        list.deinit();
    }
    try scanDir(testing.allocator, testing.io, ext_root, .user, &list);

    try testing.expectEqual(@as(usize, 2), list.items.len);

    std.mem.sort(Found, list.items, {}, struct {
        fn lt(_: void, a: Found, b: Found) bool {
            return std.mem.lessThan(u8, a.name, b.name);
        }
    }.lt);

    try testing.expectEqualStrings("alpha", list.items[0].name);
    try testing.expect(list.items[0].package_root == null);
    try testing.expect(std.mem.endsWith(u8, list.items[0].script_path, "alpha.lua"));

    try testing.expectEqualStrings("beta", list.items[1].name);
    try testing.expect(list.items[1].package_root != null);
    try testing.expect(std.mem.endsWith(u8, list.items[1].script_path, "init.lua"));
}

test "duplicate name in same directory is an error" {
    var tmp = testing.tmpDir(.{ .iterate = true });
    defer tmp.cleanup();

    try makeDir(tmp.dir, "ext_root/foo");
    try writeFile(tmp.dir, "ext_root/foo.lua", "-- single\n");
    try writeFile(tmp.dir, "ext_root/foo/init.lua", "-- dir\n");

    var path_buf: [std.fs.max_path_bytes]u8 = undefined;
    const n = try tmp.dir.realPathFile(testing.io, "ext_root", &path_buf);
    const ext_root = path_buf[0..n];

    var list: std.array_list.Managed(Found) = .init(testing.allocator);
    defer {
        for (list.items) |*f| f.deinit(testing.allocator);
        list.deinit();
    }

    const result = scanDir(testing.allocator, testing.io, ext_root, .project, &list);
    try testing.expectError(error.DuplicateExtensionInDirectory, result);
}

test "applyShadowing keeps the latest occurrence" {
    var list: std.array_list.Managed(Found) = .init(testing.allocator);
    defer {
        for (list.items) |*f| f.deinit(testing.allocator);
        list.deinit();
    }

    inline for (.{
        .{ "shared", "/u/shared.lua", Source.user },
        .{ "only_user", "/u/only_user.lua", Source.user },
        .{ "shared", "/p/shared.lua", Source.project },
        .{ "only_project", "/p/only_project.lua", Source.project },
    }) |row| {
        try list.append(.{
            .name = try testing.allocator.dupe(u8, row[0]),
            .script_path = try testing.allocator.dupe(u8, row[1]),
            .package_root = null,
            .source = row[2],
        });
    }

    try applyShadowing(testing.allocator, &list);
    try testing.expectEqual(@as(usize, 3), list.items.len);

    var shared_count: usize = 0;
    var shared_source: ?Source = null;
    for (list.items) |f| {
        if (std.mem.eql(u8, f.name, "shared")) {
            shared_count += 1;
            shared_source = f.source;
        }
    }
    try testing.expectEqual(@as(usize, 1), shared_count);
    try testing.expectEqual(@as(?Source, .project), shared_source);
}

test "loadFromDirs: project shadows user end-to-end (via long-lived runtime)" {
    var tmp = testing.tmpDir(.{ .iterate = true });
    defer tmp.cleanup();

    try makeDir(tmp.dir, "user_ext");
    try makeDir(tmp.dir, "project_ext");

    try writeFile(tmp.dir, "user_ext/greet.lua",
        \\panto.register_tool {
        \\  name = "greet", description = "user version",
        \\  schema = { type = "object" },
        \\  handler = function(input) return "USER" end,
        \\}
    );
    try writeFile(tmp.dir, "project_ext/greet.lua",
        \\panto.register_tool {
        \\  name = "greet", description = "project version",
        \\  schema = { type = "object" },
        \\  handler = function(input) return "PROJECT" end,
        \\}
    );

    var path_buf: [std.fs.max_path_bytes]u8 = undefined;
    const user_len = try tmp.dir.realPathFile(testing.io, "user_ext", &path_buf);
    const user_path = try testing.allocator.dupe(u8, path_buf[0..user_len]);
    defer testing.allocator.free(user_path);

    const proj_len = try tmp.dir.realPathFile(testing.io, "project_ext", &path_buf);
    const proj_path = try testing.allocator.dupe(u8, path_buf[0..proj_len]);
    defer testing.allocator.free(proj_path);

    var rt = try LuaRuntime.create(testing.allocator);
    defer rt.deinit();

    const n_tools = try loadFromDirs(testing.allocator, testing.io, rt, user_path, proj_path);
    try testing.expectEqual(@as(usize, 1), n_tools);

    // Invoke the tool through the source and verify the project handler ran.
    var src = rt.toolSource();
    const calls = [_]panto.ToolCall{.{ .tool_name = "greet", .input = "{}" }};
    var results: [1]panto.ToolCallResult = .{.{ .err = error.SourceDroppedCall }};
    try src.vtable.invoke_batch(src.ctx, &calls, &results, testing.allocator);
    defer testing.allocator.free(results[0].ok);
    try testing.expectEqualStrings("PROJECT", results[0].ok);
}

test "loadFromDirs: tool-name collision between extensions errors" {
    var tmp = testing.tmpDir(.{ .iterate = true });
    defer tmp.cleanup();

    try makeDir(tmp.dir, "ext");
    try writeFile(tmp.dir, "ext/alpha.lua",
        \\panto.register_tool {
        \\  name = "clash", description = "a",
        \\  schema = { type = "object" },
        \\  handler = function(input) return "a" end,
        \\}
    );
    try writeFile(tmp.dir, "ext/beta.lua",
        \\panto.register_tool {
        \\  name = "clash", description = "b",
        \\  schema = { type = "object" },
        \\  handler = function(input) return "b" end,
        \\}
    );

    var path_buf: [std.fs.max_path_bytes]u8 = undefined;
    const n = try tmp.dir.realPathFile(testing.io, "ext", &path_buf);
    const ext_path = try testing.allocator.dupe(u8, path_buf[0..n]);
    defer testing.allocator.free(ext_path);

    var rt = try LuaRuntime.create(testing.allocator);
    defer rt.deinit();

    const result = loadFromDirs(testing.allocator, testing.io, rt, null, ext_path);
    try testing.expectError(error.DuplicateTool, result);
}