Everything that was uncommitted in the shared tree, in one commit because the changes are interleaved across the same files (lcm, engine, frontend, locales). lcm: a continuation prompt is not the task. After /resume the newest user message is "Continue with the task ...", and pinning it kept the keystroke while the question it continues was cut away -- measured: a resumed session woke up with the prompt, one tool call and its error, and no task at all. The pin now skips the harness's own continuation prompts, so the real question is pinned again. test_compactor drives it: with the skip disabled the task is lost (red), with it the task comes back after the summary (green). engine: /self-check and the exact tool-argument parse are in a1794fb; this commit carries the tests for them (a failed tool observation must appear in the self-check prompt; mcp_parse_exact rejects two objects in one string) and the i18n keys the new code uses: cmd.script.*, main.no_profile, tg.busy, tg.interview.running, ui.agent_busy in all three catalogues. The rest is the in-flight work from the shared session: engine/frontend, plugins, telegram/a2a, and their tests. make check: all tests passed; check_i18n ok, 204 keys in 3 catalogues.
243 lines
6.5 KiB
C
243 lines
6.5 KiB
C
/*
|
|
* splinter -- model markdown as Telegram HTML.
|
|
*
|
|
* The chat used to show "**fett**", "|---|" and "<b>" as characters. These
|
|
* cases pin what has to become a tag, what has to stay text (snake_case, a bare
|
|
* '<'), and how a long answer is split so that every piece parses on its own.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "tgmd.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static void
|
|
check_render(const char *md, const char *want)
|
|
{
|
|
struct sp_buf b;
|
|
|
|
if (sp_buf_init(&b) != 0) {
|
|
fprintf(stderr, "FAIL init\n");
|
|
failures++;
|
|
return;
|
|
}
|
|
if (sp_tgmd_render(md, &b) != 0) {
|
|
fprintf(stderr, "FAIL render of:\n%s\n", md);
|
|
failures++;
|
|
} else if (b.p == NULL || strcmp(b.p, want) != 0) {
|
|
fprintf(stderr, "FAIL %s:%d:\n--- in ---\n%s--- want ---\n%s"
|
|
"--- got ---\n%s\n", __FILE__, __LINE__, md, want,
|
|
b.p != NULL ? b.p : "(null)");
|
|
failures++;
|
|
}
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
/* tag names opened and closed in 's', in order: 1 when every tag balances */
|
|
static int
|
|
balanced(const char *s)
|
|
{
|
|
char stack[32][16];
|
|
size_t depth = 0;
|
|
size_t i;
|
|
|
|
for (i = 0; s[i] != '\0'; i++) {
|
|
char name[16];
|
|
size_t n = 0, j;
|
|
|
|
if (s[i] != '<')
|
|
continue;
|
|
j = i + 1;
|
|
if (s[j] == '/') {
|
|
j++;
|
|
while (s[j] != '\0' && s[j] != '>')
|
|
name[n++] = s[j++];
|
|
name[n] = '\0';
|
|
if (depth == 0 || strcmp(stack[depth - 1], name) != 0)
|
|
return 0;
|
|
depth--;
|
|
i = j;
|
|
continue;
|
|
}
|
|
while (s[j] != '\0' && s[j] != '>' && s[j] != ' ')
|
|
name[n++] = s[j++];
|
|
name[n] = '\0';
|
|
if (n == 0)
|
|
continue;
|
|
if (depth == sizeof(stack) / sizeof(stack[0]))
|
|
return 0;
|
|
snprintf(stack[depth++], sizeof(stack[0]), "%s", name);
|
|
i = j;
|
|
}
|
|
return depth == 0;
|
|
}
|
|
|
|
/* the text between tags, with the entities the renderer writes undone */
|
|
static int
|
|
plain_into(struct sp_buf *out, const char *s)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; s[i] != '\0'; i++) {
|
|
if (s[i] == '<') {
|
|
const char *gt = strchr(s + i, '>');
|
|
|
|
if (gt == NULL)
|
|
return -1;
|
|
i = (size_t)(gt - s);
|
|
continue;
|
|
}
|
|
if (strncmp(s + i, "&", 5) == 0) {
|
|
if (sp_buf_puts(out, "&") != 0)
|
|
return -1;
|
|
i += 4;
|
|
} else if (strncmp(s + i, "<", 4) == 0) {
|
|
if (sp_buf_puts(out, "<") != 0)
|
|
return -1;
|
|
i += 3;
|
|
} else if (strncmp(s + i, ">", 4) == 0) {
|
|
if (sp_buf_puts(out, ">") != 0)
|
|
return -1;
|
|
i += 3;
|
|
} else if (sp_buf_add(out, s + i, 1) != 0) {
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
check_chunks(void)
|
|
{
|
|
struct sp_buf md, html, piece, all, back;
|
|
struct sp_tgmd_cursor cur;
|
|
size_t i;
|
|
int n, pieces = 0;
|
|
|
|
CHECK(sp_buf_init(&md) == 0);
|
|
CHECK(sp_buf_init(&html) == 0);
|
|
CHECK(sp_buf_init(&piece) == 0);
|
|
CHECK(sp_buf_init(&all) == 0);
|
|
CHECK(sp_buf_init(&back) == 0);
|
|
for (i = 0; i < 120; i++)
|
|
CHECK(sp_buf_printf(&md,
|
|
"**Zeile %zu** mit `code` und einem <tag> & mehr Text, "
|
|
"damit die Antwort lang genug fuer mehrere Nachrichten ist.\n",
|
|
i) == 0);
|
|
CHECK(sp_tgmd_render(md.p, &html) == 0);
|
|
CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0);
|
|
while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0) {
|
|
pieces++;
|
|
CHECK(piece.len > 0);
|
|
/* a piece is what Telegram gets: it has to fit and to parse */
|
|
CHECK(piece.len < 4096);
|
|
CHECK(balanced(piece.p));
|
|
CHECK(plain_into(&all, piece.p) == 0);
|
|
}
|
|
CHECK(n == 0);
|
|
CHECK(pieces > 1);
|
|
CHECK(plain_into(&back, html.p) == 0);
|
|
CHECK(all.p != NULL && back.p != NULL && strcmp(all.p, back.p) == 0);
|
|
sp_tgmd_cursor_free(&cur);
|
|
sp_buf_free(&md);
|
|
sp_buf_free(&html);
|
|
sp_buf_free(&piece);
|
|
sp_buf_free(&all);
|
|
sp_buf_free(&back);
|
|
}
|
|
|
|
/* A link whose tag alone is longer than a piece once looped forever. */
|
|
static void
|
|
check_huge_link(void)
|
|
{
|
|
struct sp_buf md, html, piece;
|
|
struct sp_tgmd_cursor cur;
|
|
size_t i;
|
|
int n, pieces = 0;
|
|
|
|
CHECK(sp_buf_init(&md) == 0);
|
|
CHECK(sp_buf_init(&html) == 0);
|
|
CHECK(sp_buf_init(&piece) == 0);
|
|
CHECK(sp_buf_puts(&md, "vorher **[x](https://example.org/") == 0);
|
|
for (i = 0; i < 4000; i++)
|
|
CHECK(sp_buf_puts(&md, "a") == 0);
|
|
CHECK(sp_buf_puts(&md, ")** nachher") == 0);
|
|
CHECK(sp_tgmd_render(md.p, &html) == 0);
|
|
CHECK(sp_tgmd_cursor_init(&cur, html.p) == 0);
|
|
while ((n = sp_tgmd_next(&cur, 1000, &piece)) > 0 && pieces < 100) {
|
|
pieces++;
|
|
CHECK(piece.len < 4096);
|
|
CHECK(balanced(piece.p));
|
|
}
|
|
CHECK(n == 0);
|
|
CHECK(pieces < 100);
|
|
sp_tgmd_cursor_free(&cur);
|
|
sp_buf_free(&md);
|
|
sp_buf_free(&html);
|
|
sp_buf_free(&piece);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
/* inline: the shapes a model actually writes */
|
|
check_render("**fett**", "<b>fett</b>");
|
|
check_render("__fett__", "<b>fett</b>");
|
|
check_render("*kursiv*", "<i>kursiv</i>");
|
|
check_render("_kursiv_", "<i>kursiv</i>");
|
|
check_render("~~weg~~", "<s>weg</s>");
|
|
check_render("ein `code` hier", "ein <code>code</code> hier");
|
|
check_render("**fett mit *kursiv***",
|
|
"<b>fett mit <i>kursiv</i></b>");
|
|
check_render("[Titel](https://example.org/a?x=1&y=2)",
|
|
"<a href=\"https://example.org/a?x=1&y=2\">Titel</a>");
|
|
check_render("<https://example.org/x>",
|
|
"<a href=\"https://example.org/x\">https://example.org/x</a>");
|
|
check_render("",
|
|
"<a href=\"https://example.org/b.png\">Bild</a>");
|
|
|
|
/* what must stay text */
|
|
check_render("snake_case_name", "snake_case_name");
|
|
check_render("2 * 3 * 4 = 24", "2 * 3 * 4 = 24");
|
|
check_render("a < b & c > d", "a < b & c > d");
|
|
check_render("<script>x</script>", "<script>x</script>");
|
|
check_render("[kein link](javascript:alert(1))",
|
|
"[kein link](javascript:alert(1))");
|
|
check_render("* nicht fett", "\xe2\x80\xa2 nicht fett");
|
|
|
|
/* blocks */
|
|
check_render("## Titel\nText\n", "<b>Titel</b>\nText\n");
|
|
check_render("> zitat\n> mehr\n",
|
|
"<blockquote>zitat\nmehr\n</blockquote>\n");
|
|
check_render("---\n", "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
|
"\xe2\x94\x80\n");
|
|
check_render("- eins\n- zwei\n",
|
|
"\xe2\x80\xa2 eins\n\xe2\x80\xa2 zwei\n");
|
|
check_render("```c\nif (a < b)\n\treturn **x**;\n```\n",
|
|
"<pre><code class=\"language-c\">if (a < b)\n\treturn **x**;"
|
|
"</code></pre>\n");
|
|
check_render("```\nplain & simple\n```\n",
|
|
"<pre><code>plain & simple</code></pre>\n");
|
|
|
|
/* a table: columns in <pre>, not a row of pipes in a proportional font */
|
|
check_render("| a | b |\n|---|---|\n| 1 | 22 |\n",
|
|
"<pre>| a | b |\n|---|----|\n| 1 | 22 |\n</pre>\n");
|
|
|
|
check_chunks();
|
|
check_huge_link();
|
|
|
|
if (failures == 0)
|
|
printf("test_tgmd: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|