Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(memory): support safe template filters and restore default state (#5150)
agent523fc69ccaa2
diff --git a/docs/en/api/08-admin.md b/docs/en/api/08-admin.mdindex b5de72d82..65fa106bb 100644--- a/docs/en/api/08-admin.md+++ b/docs/en/api/08-admin.md@@ -218,8 +218,15 @@ objects can be submitted unchanged: locked values matching defaults are acceptedChanged locked values, unknown keys/fields and duplicate field names return`INVALID_ARGUMENT` without modifying the active file. To edit one description whilepreserving all other customizations, GET `effective`, modify that object, then-PUT it. An empty object publishes a complete copy of the defaults as custom;-DELETE removes the custom file. DELETE is idempotent.+PUT it. If the completed, validated configuration exactly matches deployment+defaults (before publication metadata is added), PUT removes that type's override+and returns `status=system_default`, `updated_at=null`. This includes an empty+object, an unchanged default form, or saving after restoring all edited fields+to their defaults. Any remaining difference keeps the template `custom`;+whitespace and newline differences in descriptions or bodies are not ignored.+Once the override is removed, subsequent extractions follow deployment defaults;+previously captured extraction snapshots are unchanged. DELETE always removes+the type's override. Repeated default PUTs and DELETEs are idempotent.Results contain `memory_type`, `status` (`system_default` or `custom`),`updated_at` (UTC publication time or null), and full `defaults` / `effective`@@ -290,7 +297,7 @@ disable rendering, and no description provenance flag is stored or checked.Only the existing `language` context variable is available; body fields,`extract_context`, and arbitrary objects are not exposed. The syntax subset isthe same as the restricted bodies below: conditionals, local variables, bounded-literal loops, safe string methods and tests, but no filters or arbitrary calls.+literal loops, safe string methods, approved string filters and tests, but no arbitrary calls.For example, <code v-pre>Use {{ language.upper() }}.</code> renders as `Use EN.` when the existingschema-rendering context supplies `language=en`. No new language propagation isintroduced; the Python protocol's existing static field-description path remains@@ -349,8 +356,17 @@ may call these read-only `extract_context` helpers with positional arguments:`get_first_message_time_with_weekday_from_ranges(ranges)`,`get_event_content(ranges, summary[, ratio_threshold])`,`get_year(ranges)`, `get_month(ranges)`, `get_day(ranges)`.-The first argument must be `ranges` directly; missing field values are supplied as-empty strings, so no fallback filter is needed. An explicit ratio must be a numeric+The first argument may use any expression allowed by the same syntax sandbox,+including local aliases, conditionals and approved filter chains. Immediately+before each helper call, its evaluated value must be a plain string exactly equal+to the current memory's original `ranges`, or an empty string (no source messages).+For example, `ranges | default('') | trim` works when it leaves the value unchanged;+`{% set selected = ranges %}` can be followed by `get_year(selected)`.+No normalization is performed when comparing ranges. Missing field values are+already supplied as empty strings. Publication checks syntax without executing+helpers; a changed or non-string range value fails at rendering with+`content_template: invalid_ranges`, before the helper reads any messages, and stops+that memory file write. An explicit ratio must be a numericliteral from 0 to 1 (omitted: 0.2; built-in: 0).Supported Jinja: `if/elif/else`, comparisons/boolean expressions, local `set`, and@@ -364,10 +380,18 @@ string subclasses) are not allowed. Methods can be chained or used on string fielocals, literals, and string results of approved Events helpers. Method referencescannot be stored or accessed without calling them.-No filters are supported in custom Account bodies, including `| upper`,-`| lower`, `| trim`, `| default(...)`, or `| length`; this unreleased interface does-not retain a filter compatibility mode. Use `summary or 'pending'` or an explicit-conditional for fallbacks, and `summary.strip().upper()` for string formatting.+String filters: `| upper`, `| lower`, and `| trim`, equivalent to `.upper()`,+`.lower()`, and `.strip()`. Like the methods, they accept only plain strings and+no positional or keyword arguments. They can be chained or mixed with methods,+for example `summary | trim | upper` or `summary.strip() | upper`.+The `default` filter accepts no argument or one literal string, for example+`| default` / `| default()` / `| default('N/A')`. It replaces only undefined values;+empty strings and `None` remain unchanged, matching the built-in Events template.+It accepts only plain strings, `None` or undefined values without object coercion.+The boolean argument, keyword arguments and expanded/dynamic arguments are not+supported. Use `summary or 'pending'` or an explicit conditional for empty-value+fallbacks. Other filters, including `| length`, `| d(...)` and `| attr(...)`, remain+unsupported.Tests: `defined`, `undefined`, `none`, `string` remain supported.Built-in field/context names cannot be overwritten. Imports,inheritance, macros, arbitrary calls/attributes, subscripts, arithmetic/string@@ -376,6 +400,9 @@ multiplication/concatenation and reserved `MEMORY_FIELDS` comments are not allowLimits: 64 KiB UTF-8 source, 2048 AST nodes, 1 MiB rendered body excluding systemmetadata. Nonmatching Account bodies are checked at publication and extraction load,then rendered with a restricted Jinja environment and only approved fields/helpers.+The built-in Events, Soul and Identity bodies also pass this restricted syntax,+including after edits to headings, trailing newlines or CRLF line endings. These+edits do not bypass validation or mark the edited body as deployment-owned.Runtime failures on this restricted path stop that file write instead of fallingback to an empty body. Exact inherited bodies keep the deployment renderer'sexisting behavior, including its error/fallback semantics; they are not subject todiff --git a/docs/zh/api/08-admin.md b/docs/zh/api/08-admin.mdindex e34e58103..5c25bb207 100644--- a/docs/zh/api/08-admin.md+++ b/docs/zh/api/08-admin.md@@ -208,8 +208,12 @@ PUT 从**部署默认模板**补齐未传入的配置,不从上一次 Account完整 GET `effective` 对象可以回传:锁定字段值与默认值相同则接受,任何锁定值变更、未知配置项、未知字段或重复字段名均返回 `INVALID_ARGUMENT`,当前生效文件不变。若仅调整一个 description 且需保留其他自定义内容,应先 GET,修改 `effective` 对象后-整体 PUT。空对象会发布一份完整默认配置,状态仍为自定义;恢复系统默认应调用 DELETE。-DELETE 幂等。+整体 PUT。补齐并校验后的完整配置若与部署默认值完全一致(不含发布时间元数据),+PUT 会移除该类型的自定义覆盖,返回 `status=system_default`、`updated_at=null`。+这包括提交空对象、原样提交默认表单,以及在编辑页逐项恢复默认后保存;只要还有任意配置+不同,就继续返回 `custom`。比较不忽略说明或正文中的空格、换行等内容差异。+移除覆盖后,后续抽取跟随部署默认模板;此前已取得的抽取快照不变。+DELETE 始终移除该类型的覆盖;重复 PUT 默认配置或 DELETE 都是幂等的。返回包含 `memory_type`、`status`(`system_default` / `custom`)、`updated_at`(UTC 发布时间,默认状态为 null),以及完整的 `defaults` / `effective`。@@ -257,7 +261,7 @@ Registry;无关类型变更不会触发拆批。不同 Schema 分开合并、白名单内提交的说明和正文模板必须是非空字符串;单文件序列化后不超过 1 MiB。`description`(类型说明及 `fields[].description`)统一支持受限 Jinja,不因来自部署默认值或账户覆盖而改变规则,不再记录或检查说明来源标志。-仅开放已有上下文中的 `language`,不开放正文变量、`extract_context` 或任意对象。语法复用下节受限正文的条件、局部变量、有界字面量循环、安全字符串方法及测试;不支持过滤器或任意调用。+仅开放已有上下文中的 `language`,不开放正文变量、`extract_context` 或任意对象。语法复用下节受限正文的条件、局部变量、有界字面量循环、安全字符串方法、白名单字符串过滤器及测试;不支持任意调用。例如已有 Schema 渲染上下文提供 `language=en` 时,<code v-pre>请使用 {{ language.upper() }}。</code> 会展开为 `请使用 EN。`,用户修改文字不会让变量停止展开。本次不新增语言传递链路,Python 协议原有的静态字段说明展示路径保持不变。缺失语言时保留原来的 undefined/空字符串行为,可用 `language or '中文'` 提供回退;上下文值中的 Jinja 不会被递归执行。越界的自定义表达式在保存前拒绝,已保存说明在抽取加载时重新校验;部署说明渲染也受同样限制,已有部署若使用白名单外语法,需要调整,不能凭来源绕过限制。@@ -299,7 +303,10 @@ Events 的默认 embedding 模板引用正文,因此正文变化也可能影- `get_event_content(ranges, summary[, ratio_threshold])`:按已有逻辑选择 ChatLog/摘要;省略阈值为 0.2,显式 0 表示存在原文时优先原文。- `get_year(ranges)`、`get_month(ranges)`、`get_day(ranges)`:来源日期分量。-首个参数直接使用 `ranges`,不能自行构造消息范围;缺失字段会传入空字符串,无需过滤器兜底。+首个参数可使用统一语法白名单内的表达式,包括局部变量、条件表达式及允许的过滤器链。+每次实际调用方法前,参数求值结果必须是普通字符串,且与当前记忆原始 `ranges` 完全相等,或为空字符串(不读取来源消息)。+例如 `ranges | default('') | trim` 在结果未改变时可用;也可先 `{% set selected = ranges %}`,再调用 `get_year(selected)`。+比较范围时不做归一化;缺失字段本来就会传入空字符串。发布时仅校验语法,不执行方法;改变范围或传入非字符串会在实际渲染时返回 `content_template: invalid_ranges`,在方法读取消息前拒绝,并停止该次记忆文件写入。阈值只能为 0~1 的数字字面量。允许去掉 ChatLog 或资源事件分支,但去掉后不再自动展示这些正文/资源链接;原始 Session 仍保留。@@ -309,12 +316,15 @@ Events 的默认 embedding 模板引用正文,因此正文变化也可能影- `for` 遍历模板中显式写出的列表/元组,最多 32 项;支持标题/字段二元组和 `loop.index/index0/first/last/length`。不支持嵌套/递归循环、range() 或遍历消息/长字符串。- 字符串方法:`.upper()`、`.lower()`、`.strip()`,均不接受位置参数或关键字参数。可用于字符串字段、局部变量、字面量、Events 白名单方法返回的字符串,并支持链式调用,例如 `summary.strip().upper()`。- 方法语法与部署模板一致,但账户正文只开放上述少数方法;读取属性前先检查接收者必须是普通字符串,其他对象(包括字符串子类)的同名方法/属性不能借此被调用。也不允许只取出方法引用、保存后再调用。-- 自定义正文不支持任何过滤器,包括 `| upper`、`| lower`、`| trim`、`| default(...)`、`| length`;该接口尚未上线,不为自定义正文保留过滤器兼容模式。空值回退使用 `summary or '待补充'` 或条件表达式。+- 字符串过滤器:`| upper`、`| lower`、`| trim`,分别等价于 `.upper()`、`.lower()`、`.strip()`。同样只接受普通字符串,不接受位置参数或关键字参数。支持链式调用及与方法混用,例如 `summary | trim | upper` 或 `summary.strip() | upper`。+- `default` 过滤器:支持无参数或一个字符串字面量,例如 `| default` / `| default()` / `| default('N/A')`。只替换未定义值,不替换空字符串或 `None`,与内置 Events 模板保持一致。接收者仅允许普通字符串、`None` 或未定义值,不转换任意对象;不支持第二个布尔参数、关键字参数或展开/动态参数。空值回退使用 `summary or '待补充'` 或条件表达式。+- 其他过滤器仍不支持,包括 `| length`、`| d(...)`、`| attr(...)`。- 测试:`defined`、`undefined`、`none`、`string`。- 不支持模板导入/继承、宏、任意函数/对象属性访问、下标访问、算术或字符串倍增/拼接。不能注入系统保留的 `<!-- MEMORY_FIELDS ... -->` 元数据。模板 UTF-8 大小 ≤ 64 KiB,AST 节点 ≤ 2048,渲染正文 ≤ 1 MiB(不含系统追加元数据)。与部署默认值不同的 Account 正文在发布时和抽取加载时验证,运行时使用受限 Jinja 环境,只提供白名单字段/方法。+内置 Events、Soul、Identity 正文也满足受限语法;仅修改标题、末尾换行或 CRLF 换行后仍可校验发布。这些改动不会绕过校验,也不会被标记为部署原样正文。受限路径渲染失败会报告错误并停止该次文件写入,不走旧的空正文 fallback。原样继承的正文继续使用部署渲染器,包括原有错误/fallback 语义,不受上述受限渲染器的源码、AST、正文输出上限约束;完整账户 YAML 仍受 1 MiB 上限约束。diff --git a/openviking/session/memory/account_templates.py b/openviking/session/memory/account_templates.pyindex 4816c9cb5..803c083e1 100644--- a/openviking/session/memory/account_templates.py+++ b/openviking/session/memory/account_templates.py@@ -322,10 +322,16 @@ async def update_account_memory_template() from excexcept (ValueError, TypeError, KeyError, AttributeError, TemplateSyntaxError) as exc:raise InvalidArgumentError(f"Invalid memory template: {exc}") from exc- complete["_updated_at"] = datetime.now(timezone.utc).isoformat()- encoded = yaml.safe_dump(complete, allow_unicode=True, sort_keys=False).encode("utf-8")- if len(encoded) > _MAX_CONFIG_BYTES:- raise InvalidArgumentError("A memory template must not exceed 1 MiB")+ # Saving an unchanged default form (including per-field resets) removes+ # the override through the same locked path as DELETE. Compare before+ # adding publication metadata; any remaining edit keeps it custom.+ if complete == defaults:+ complete = None+ else:+ complete["_updated_at"] = datetime.now(timezone.utc).isoformat()+ encoded = yaml.safe_dump(complete, allow_unicode=True, sort_keys=False).encode("utf-8")+ if len(encoded) > _MAX_CONFIG_BYTES:+ raise InvalidArgumentError("A memory template must not exceed 1 MiB")path = account_memory_template_path(account_id, memory_type)staging_path = f"{path}.{uuid.uuid4().hex}.tmp" if complete is not None else Nonediff --git a/openviking/session/memory/utils/content_template.py b/openviking/session/memory/utils/content_template.pyindex f3fccd0dd..44f229977 100644--- a/openviking/session/memory/utils/content_template.py+++ b/openviking/session/memory/utils/content_template.py@@ -10,9 +10,10 @@ both at publication and when rendering the extraction snapshot.from __future__ import annotationsimport re-from typing import Any, Mapping+from functools import partial+from typing import Any, Callable, Mapping-from jinja2 import StrictUndefined, TemplateError, meta, nodes+from jinja2 import StrictUndefined, TemplateError, Undefined, meta, nodesfrom jinja2.runtime import LoopContextfrom jinja2.sandbox import ImmutableSandboxedEnvironment@@ -34,6 +35,7 @@ _EVENT_METHODS = {"get_day": (1,),}_STRING_METHODS = {"upper", "lower", "strip"}+_STRING_FILTERS = {"upper": "upper", "lower": "lower", "trim": "strip"}_TESTS = {"defined", "undefined", "none", "string"}_LOOP_ATTRIBUTES = {"index", "index0", "first", "last", "length"}_RESERVED_METADATA = re.compile(r"<!--\s*MEMORY_FIELDS\b")@@ -50,6 +52,7 @@ _NODES = (nodes.Tuple,nodes.Getattr,nodes.Call,+ nodes.Filter,nodes.Test,nodes.Compare,nodes.Operand,@@ -81,10 +84,31 @@ class _ContentEnvironment(ImmutableSandboxedEnvironment):)+def _string_filter(method: str, value: Any) -> str:+ # Unlike Jinja's built-ins, do not coerce arbitrary objects through __str__+ # or invoke overridden methods on string subclasses.+ if type(value) is not str:+ raise TemplateError("String filters require a plain string")+ return getattr(value, method)()+++def _default_filter(value: Any, default_value: str = "") -> str | None:+ # Match Jinja's undefined-only fallback, including None/empty-string behavior.+ # Do not test truthiness or coerce objects supplied by context helpers.+ if isinstance(value, Undefined):+ return default_value+ if value is None or type(value) is str:+ return value+ raise TemplateError("Default filter requires a plain string, None or undefined")++def _environment() -> _ContentEnvironment:env = _ContentEnvironment(autoescape=False, undefined=StrictUndefined)env.globals.clear()- env.filters.clear()+ env.filters = {+ name: partial(_string_filter, method) for name, method in _STRING_FILTERS.items()+ }+ env.filters["default"] = _default_filterenv.tests = {name: env.tests[name] for name in _TESTS}return env@@ -126,7 +150,18 @@ def _parse()for node in all_nodes:if isinstance(node, nodes.Filter):- raise ContentTemplateError("unsupported_filter", node.lineno)+ if node.name not in {*_STRING_FILTERS, "default"}:+ raise ContentTemplateError("unsupported_filter", node.lineno)+ if node.kwargs or node.dyn_args or node.dyn_kwargs:+ raise ContentTemplateError("invalid_arguments", node.lineno)+ if node.name == "default":+ if len(node.args) > 1 or any(+ not isinstance(arg, nodes.Const) or type(arg.value) is not str+ for arg in node.args+ ):+ raise ContentTemplateError("invalid_arguments", node.lineno)+ elif node.args:+ raise ContentTemplateError("invalid_arguments", node.lineno)if not isinstance(node, _NODES):raise ContentTemplateError("unsupported_syntax", node.lineno)if isinstance(node, nodes.Name) and node.ctx == "store":@@ -214,10 +249,8 @@ def _validate_call(node: nodes.Call, memory_type: str) -> None:or len(node.args) not in _EVENT_METHODS[target.attr]):raise ContentTemplateError("invalid_arguments", node.lineno)- # Never allow a template to fabricate an unbounded message-index range.- ranges = node.args[0]- if not isinstance(ranges, nodes.Name) or ranges.name != "ranges":- raise ContentTemplateError("invalid_ranges", node.lineno)+ # Range expressions follow the same syntax rules as other expressions.+ # Their evaluated values are checked before calling an extraction helper.if len(node.args) == 3:ratio = node.args[2]if (@@ -228,6 +261,28 @@ def _validate_call(node: nodes.Call, memory_type: str) -> None:raise ContentTemplateError("invalid_ratio", node.lineno)+def _call_event_helper(+ env: _ContentEnvironment,+ helper: Callable[..., Any],+ original_ranges: str,+ ranges: Any,+ *args: Any,+) -> Any:+ # Validate values, not a particular AST shape: aliases, conditions and+ # filters are fine, but cannot change which source messages a helper reads.+ # Check exact types before equality to avoid invoking user-defined methods.+ if (+ type(ranges) is not str+ or type(original_ranges) is not str+ or ranges not in ("", original_ranges)+ ):+ raise ContentTemplateError("invalid_ranges")+ # Wrapping a helper must not bypass its original sandbox callable flags.+ if not env.is_safe_callable(helper):+ raise TemplateError("Unsafe event helper")+ return helper(ranges, *args)++def validate_content_template(template: str, memory_type: str) -> None:_parse(template, memory_type, _environment())@@ -248,7 +303,7 @@ def render_content_template(raise ContentTemplateError("invalid_field_value")if memory_type == "events":values["extract_context"] = {- name: getattr(extract_context, name)+ name: partial(_call_event_helper, env, getattr(extract_context, name), values["ranges"])for name in _EVENT_METHODSif extract_context is not None and hasattr(extract_context, name)}diff --git a/tests/server/test_admin_api.py b/tests/server/test_admin_api.pyindex d5399d6c9..7ff95c6f3 100644--- a/tests/server/test_admin_api.py+++ b/tests/server/test_admin_api.py@@ -8,6 +8,7 @@ import hashlibimport jsonimport threadingimport uuid+from copy import deepcopyfrom unittest.mock import AsyncMock, Mockimport httpx@@ -37,7 +38,16 @@ from openviking.service.task_store import (SYSTEM_TASK_USER_ID,)from openviking.service.task_tracker import get_task_tracker-from openviking.session.memory.account_templates import EDITABLE_MEMORY_TEMPLATE_FIELDS+from openviking.session.memory.account_templates import (+ EDITABLE_MEMORY_TEMPLATE_FIELDS,+ account_memory_template_path,+ resolve_account_memory_registry,+)+from openviking.session.memory.extract_loop import ExtractLoop+from openviking.session.memory.memory_isolation_handler import MemoryIsolationHandler+from openviking.session.memory.memory_type_registry import MemoryTypeRegistry, get_default_registry+from openviking.session.memory.patch_merge_context_provider import PatchMergeContextProvider+from openviking.session.memory.session_extract_context_provider import SessionExtractContextProviderfrom openviking_cli.exceptions import OpenVikingErrorfrom openviking_cli.session.user_id import UserIdentifierfrom openviking_cli.utils.config import get_openviking_config@@ -422,17 +432,244 @@ async def test_account_memory_templates_publish_and_reset()assert default_form.status_code == 200, default_form.textassert default_form.json()["result"]["effective"] == result["defaults"]+ assert default_form.json()["result"]["status"] == "system_default"+ assert default_form.json()["result"]["updated_at"] is None+ assert path not in fs.agfs._files+++ "memory_type,edit",+ [+ (kind, edit)+ for kind in EDITABLE_MEMORY_TEMPLATE_FIELDS+ for edit in ("exact", "description", "field_description")+ ]+ + [+ (kind, edit)+ for kind in ("events", "soul", "identity")+ for edit in ("rstrip", "newline", "crlf", "heading")+ ],+)+async def test_account_memory_templates_default_form_roundtrip(+ lightweight_admin_client,+ lightweight_admin_app,+ template_account,+ memory_type,+ edit,+):+ from openviking.session.memory.dataclass import MemoryFile+ from openviking.session.memory.memory_updater import ExtractContext+ from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils+ from openviking.session.memory.utils.template_utils import TemplateUtils++ account_id, headers = template_account+ client = lightweight_admin_client+ url = f"/api/v1/admin/accounts/{account_id}/memory-templates/{memory_type}"+ initial = await client.get(url, headers=headers)+ assert initial.status_code == 200, initial.text+ assert initial.json()["result"]["status"] == "system_default"+ defaults = initial.json()["result"]["defaults"]+ # Match the managed form: only editable fields from the actual GET result.+ body = {+ "description": defaults["description"],+ "fields": [+ {"name": f["name"], "description": f["description"]}+ for f in defaults["fields"]+ if f["name"] in EDITABLE_MEMORY_TEMPLATE_FIELDS[memory_type]+ ],+ }+ if memory_type in ("events", "soul", "identity"):+ original = defaults["content_template"]+ body["content_template"] = {+ "rstrip": original.rstrip(),+ "newline": original + "\n",+ "crlf": original.replace("\n", "\r\n"),+ "heading": "# Account memory\n" + original,+ }.get(edit, original)+ if edit == "description":+ body["description"] += "\nAccount instructions."+ elif edit == "field_description":+ body["fields"][0]["description"] += "\nAccount field instructions."+ published = await client.put(url, json=body, headers=headers)+ assert published.status_code == 200, published.text+ result = published.json()["result"]+ assert result["defaults"] == defaults+ effective = result["effective"]+ is_default = effective == defaults+ assert result["status"] == ("system_default" if is_default else "custom")+ assert (result["updated_at"] is None) == is_default+ assert effective["description"] == body["description"]+ for field in body["fields"]:+ actual = next(f for f in effective["fields"] if f["name"] == field["name"])+ assert actual["description"] == field["description"]+ assert (await client.get(url, headers=headers)).json()["result"] == result+ roundtrip = await client.put(url, json=effective, headers=headers)+ assert roundtrip.status_code == 200, roundtrip.text+ assert roundtrip.json()["result"] == result+ fs = lightweight_admin_app.state.fake_service.viking_fs+ snapshot = await resolve_account_memory_registry(fs, account_id, get_default_registry())+ schema = snapshot.get(memory_type)+ if "content_template" in body:+ assert schema.content_template == effective["content_template"] == body["content_template"]+ assert schema._account_content_template == (body["content_template"] != original)+ values = {f.name: "Business fact" for f in schema.fields}+ values["ranges"] = ""+ context = ExtractContext([])+ rendered = MemoryFileUtils.write(+ MemoryFile(memory_type=memory_type, extra_fields=values),+ content_template=schema.content_template,+ extract_context=context,+ account_content_template_type=memory_type if schema._account_content_template else None,+ )+ assert MemoryFileUtils.read(rendered).content == TemplateUtils.render(+ body["content_template"], values, context+ )+ reset = await client.delete(url, headers=headers)+ assert reset.status_code == 200, reset.text+ assert reset.json()["result"]["effective"] == defaults+ assert reset.json()["result"]["status"] == "system_default"+++def _editable_template_form(defaults, memory_type):+ body = {+ "description": defaults["description"],+ "fields": [+ {"name": field["name"], "description": field["description"]}+ for field in defaults["fields"]+ if field["name"] in EDITABLE_MEMORY_TEMPLATE_FIELDS[memory_type]+ ],+ }+ if memory_type in ("events", "soul", "identity"):+ body["content_template"] = defaults["content_template"]+ return body+++ "memory_type,module",+ [+ (kind, module)+ for kind, fields in EDITABLE_MEMORY_TEMPLATE_FIELDS.items()+ for module in ("description", *fields)+ ]+ + [(kind, "content_template") for kind in ("events", "soul", "identity")],+)+async def test_account_memory_templates_module_reset_clears_override(+ lightweight_admin_client, lightweight_admin_app, template_account, memory_type, module+):+ account_id, headers = template_account+ client = lightweight_admin_client+ root = f"/api/v1/admin/accounts/{account_id}/memory-templates"+ url = f"{root}/{memory_type}"+ initial = (await client.get(url, headers=headers)).json()["result"]+ form = _editable_template_form(initial["defaults"], memory_type)+ body = deepcopy(form)+ if module in ("description", "content_template"):+ body[module] += "\nAccount instructions."+ else:+ field = next(field for field in body["fields"] if field["name"] == module)+ field["description"] += "\nAccount field instructions."+ publish = await client.put(url, json=body, headers=headers)+ assert publish.status_code == 200, publish.text+ result = publish.json()["result"]+ assert result["status"] == "custom"+ fs = lightweight_admin_app.state.fake_service.viking_fs+ path = account_memory_template_path(account_id, memory_type)+ previous = fs.agfs._files[path]+ snapshot = await resolve_account_memory_registry(fs, account_id, get_default_registry())+ previous_schema = snapshot.get(memory_type).model_dump()+ untouched = {key: value for key, value in fs.agfs._files.items() if key != path}++ # Reopen the editor, reset only the edited module, then save the whole form.+ reopened = (await client.get(url, headers=headers)).json()["result"]+ body = _editable_template_form(reopened["effective"], memory_type)+ if module in ("description", "content_template"):+ body[module] = form[module]+ else:+ field = next(field for field in body["fields"] if field["name"] == module)+ field["description"] = next(+ field["description"] for field in form["fields"] if field["name"] == module+ )+ # Field order does not change the completed configuration.+ body["fields"].reverse()+ restored = await client.put(url, json=body, headers=headers)+ assert restored.status_code == 200, restored.text+ assert restored.json()["result"] == initial+ assert path not in fs.agfs._files+ assert fs.agfs._files[path + ".backup"] == previous+ assert all(fs.agfs._files[key] == value for key, value in untouched.items())+ assert (await client.get(url, headers=headers)).json()["result"] == initial+ listed = (await client.get(root, headers=headers)).json()["result"]["templates"]+ assert next(item for item in listed if item["memory_type"] == memory_type) == {+ key: value for key, value in initial.items() if key != "account_id"+ }+ resolved = await resolve_account_memory_registry(fs, account_id, get_default_registry())+ assert (+ resolved.get(memory_type).model_dump()+ == get_default_registry().get(memory_type).model_dump()+ )+ assert snapshot.get(memory_type).model_dump() == previous_schema+ stored = dict(fs.agfs._files)+ repeated = await client.put(url, json=body, headers=headers)+ assert repeated.status_code == 200, repeated.text+ assert repeated.json()["result"] == initial+ assert fs.agfs._files == stored++[email protected]("memory_type", EDITABLE_MEMORY_TEMPLATE_FIELDS)+async def test_account_memory_templates_module_reset_keeps_other_edits(+ lightweight_admin_client, lightweight_admin_app, template_account, memory_type+):+ account_id, headers = template_account+ client = lightweight_admin_client+ url = f"/api/v1/admin/accounts/{account_id}/memory-templates/{memory_type}"+ initial = (await client.get(url, headers=headers)).json()["result"]+ body = _editable_template_form(initial["defaults"], memory_type)+ body["description"] += "\nType edit."+ body["fields"][0]["description"] += "\nField edit."+ response = await client.put(url, json=body, headers=headers)+ assert response.status_code == 200, response.text+ body["description"] = initial["defaults"]["description"]+ response = await client.put(url, json=body, headers=headers)+ assert response.status_code == 200, response.text+ result = response.json()["result"]+ assert result["status"] == "custom"+ assert result["updated_at"]+ assert result["effective"]["description"] == initial["defaults"]["description"]+ field = next(f for f in result["effective"]["fields"] if f["name"] == body["fields"][0]["name"])+ assert field["description"] == body["fields"][0]["description"]+ assert account_memory_template_path(account_id, memory_type) in (+ lightweight_admin_app.state.fake_service.viking_fs.agfs._files+ )+++async def test_account_memory_templates_save_clears_legacy_default_override(+ lightweight_admin_client, lightweight_admin_app, template_account+):+ account_id, headers = template_account+ client = lightweight_admin_client+ url = f"/api/v1/admin/accounts/{account_id}/memory-templates/profile"+ initial = (await client.get(url, headers=headers)).json()["result"]+ legacy = {**initial["defaults"], "_updated_at": "2026-09-01T00:00:00+00:00"}+ fs = lightweight_admin_app.state.fake_service.viking_fs+ path = account_memory_template_path(account_id, "profile")+ fs.agfs._files[path] = yaml.safe_dump(legacy).encode()+ before = (await client.get(url, headers=headers)).json()["result"]+ assert before["status"] == "custom" # Reads do not migrate persisted overrides.+ response = await client.put(url, json=before["effective"], headers=headers)+ assert response.status_code == 200, response.text+ assert response.json()["result"] == initial+ assert path not in fs.agfs._files@pytest.mark.parametrize("body",[{"description": 123},- {"description": "{{ language | upper }}"},+ {"description": "{{ language | length }}"},{"description": "{{ cycler.__init__.__globals__.__builtins__.len('harmless') }}"},{"description": "literal {{ unclosed"},{"fields": [{"name": "summary", "description": "{{ summary }}"}]},- {"fields": [{"name": "summary", "description": "{{ language | lower }}"}]},+ {"fields": [{"name": "summary", "description": "{{ language | trim('x') }}"}]},{"fields": [{"name": "summary", "description": "literal {{ unclosed"}]},{"_account_description": False},{"fields": [{"name": "summary", "_account_description": False}]},@@ -446,11 +683,11 @@ async def test_account_memory_templates_publish_and_reset({"content_template": "{{"},{"content_template": "{{ unknown_field }}"},{"content_template": "{{ extract_context.messages }}"},- {"content_template": "{{ extract_context.get_year('0-999999999') }}"},{"content_template": "{% include 'private.yaml' %}"},{"content_template": "{{ summary | attr('__class__') }}"},- {"content_template": "{{ summary | upper }}"},- {"content_template": "{{ summary | default('pending') }}"},+ {"content_template": "{{ summary | length }}"},+ {"content_template": "{{ summary | trim('x') }}"},+ {"content_template": "{{ summary | default('pending', true) }}"},{"content_template": "{{ summary.strip('x') }}"},{"content_template": "x" * (64 * 1024 + 1)},],@@ -479,7 +716,7 @@ async def test_account_memory_templates_reject_invalid_configuration(@pytest.mark.parametrize(("text", "reason"),[- ("{{ language | upper }}", "unsupported_filter"),+ ("{{ language | length }}", "unsupported_filter"),("{{ cycler.__init__.__globals__.__builtins__.len('harmless') }}", "unsupported_call"),("{{", "invalid_jinja"),],@@ -579,25 +816,110 @@ async def test_account_memory_templates_content_validation_error_details(}+ "expression",+ ["selected", "ranges|default('')|trim", "ranges|default('0-999')", "ranges if summary else ''"],+)+async def test_account_memory_templates_range_expressions_reach_file_body(+ lightweight_admin_client, lightweight_admin_app, template_account, expression+):+ from openviking.message import Message, TextPart+ from openviking.session.memory.dataclass import MemoryFile+ from openviking.session.memory.memory_updater import ExtractContext+ from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils++ account_id, headers = template_account+ url = f"/api/v1/admin/accounts/{account_id}/memory-templates/events"+ template = (+ "{% set selected = ranges %}{{ extract_context.get_event_content("+ + expression+ + ", summary, 0) }}"+ )+ published = await lightweight_admin_client.put(+ url, json={"content_template": template}, headers=headers+ )+ assert published.status_code == 200, published.text+ result = (await lightweight_admin_client.get(url, headers=headers)).json()["result"]+ assert result["effective"]["content_template"] == template+ roundtrip = await lightweight_admin_client.put(url, json=result["effective"], headers=headers)+ assert roundtrip.status_code == 200, roundtrip.text+ fs = lightweight_admin_app.state.fake_service.viking_fs+ snapshot = await resolve_account_memory_registry(fs, account_id, MemoryTypeRegistry())+ schema = snapshot.get("events")+ context = ExtractContext(+ [+ Message(id="m1", role="user", parts=[TextPart("Selected source message")]),+ Message(id="m2", role="user", parts=[TextPart("Unrelated source message")]),+ ]+ )+ rendered = MemoryFileUtils.write(+ MemoryFile(memory_type="events", extra_fields={"ranges": "0", "summary": "summary"}),+ content_template=schema.content_template,+ extract_context=context,+ account_content_template_type="events" if schema._account_content_template else None,+ )+ content = MemoryFileUtils.read(rendered).content+ assert "Selected source message" in content+ assert "Unrelated source message" not in content+++async def test_account_memory_templates_range_values_are_checked_at_render_time(+ lightweight_admin_client, lightweight_admin_app, template_account+):+ from types import SimpleNamespace++ from openviking.session.memory.dataclass import MemoryFile+ from openviking.session.memory.utils.content_template import ContentTemplateError+ from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils++ account_id, headers = template_account+ url = f"/api/v1/admin/accounts/{account_id}/memory-templates/events"+ template = "{% set selected = summary %}{{ extract_context.get_year(selected) }}"+ published = await lightweight_admin_client.put(+ url, json={"content_template": template}, headers=headers+ )+ assert published.status_code == 200, published.text+ fs = lightweight_admin_app.state.fake_service.viking_fs+ snapshot = await resolve_account_memory_registry(fs, account_id, MemoryTypeRegistry())+ schema = snapshot.get("events")+ get_year = Mock(spec=[], return_value="2026")+ with pytest.raises(ContentTemplateError, match="invalid_ranges"):+ MemoryFileUtils.write(+ MemoryFile(memory_type="events", extra_fields={"ranges": "0", "summary": "0-999"}),+ content_template=schema.content_template,+ extract_context=SimpleNamespace(get_year=get_year),+ account_content_template_type="events",+ )+ get_year.assert_not_called()++@pytest.mark.parametrize("memory_type, field_name",[("events", "summary"), ("soul", "core_truths"), ("identity", "introduction")],)-async def test_account_memory_templates_string_methods_reach_file_body([email protected]("formatting", [".strip().upper()", " | trim | upper"])+async def test_account_memory_templates_string_formatting_reaches_file_body(lightweight_admin_client,lightweight_admin_app,template_account,memory_type,field_name,+ formatting,):from openviking.session.memory.dataclass import MemoryFilefrom openviking.session.memory.utils.memory_file_utils import MemoryFileUtilsaccount_id, headers = template_accounturl = f"/api/v1/admin/accounts/{account_id}/memory-templates/{memory_type}"- body = {"content_template": "# {{ " + field_name + ".strip().upper() }}"}+ body = {"content_template": "# {{ " + field_name + formatting + " }}"}response = await lightweight_admin_client.put(url, json=body, headers=headers)assert response.status_code == 200, response.text+ effective = (await lightweight_admin_client.get(url, headers=headers)).json()["result"][+ "effective"+ ]+ assert effective["content_template"] == body["content_template"]+ roundtrip = await lightweight_admin_client.put(url, json=effective, headers=headers)+ assert roundtrip.status_code == 200, roundtrip.textfs = lightweight_admin_app.state.fake_service.viking_fssnapshot = await resolve_account_memory_registry(fs, account_id, MemoryTypeRegistry())schema = snapshot.get(memory_type)@@ -668,7 +990,9 @@ async def test_account_memory_templates_inherit_exact_deployment_body(body["description"] = "Account instructions"response = await lightweight_admin_client.put(url, json=body, headers=headers)assert response.status_code == 200, response.text- assert response.json()["result"]["status"] == "custom"+ assert response.json()["result"]["status"] == (+ "system_default" if request_kind == "empty" else "custom"+ )assert response.json()["result"]["effective"]["content_template"] == deployment_body# Roundtrip a persisted override too, not only defaults returned before a PUT.response = await lightweight_admin_client.put(@@ -676,8 +1000,13 @@ async def test_account_memory_templates_inherit_exact_deployment_body()assert response.status_code == 200, response.textfs = lightweight_admin_app.state.fake_service.viking_fs- stored = yaml.safe_load(fs.agfs._files[account_memory_template_path(account_id, memory_type)])- assert "_account_content_template" not in stored+ path = account_memory_template_path(account_id, memory_type)+ if request_kind == "empty":+ assert path not in fs.agfs._files+ assert response.json()["result"]["updated_at"] is None+ else:+ stored = yaml.safe_load(fs.agfs._files[path])+ assert "_account_content_template" not in storedsnapshot = await resolve_account_memory_registry(fs, account_id, defaults)schema = snapshot.get(memory_type)assert schema._account_content_template is False@@ -705,11 +1034,14 @@ async def test_account_memory_templates_recheck_persisted_body_without_trusting_account_id, headers = template_accountdefaults = MemoryTypeRegistry()- deployment_body = "{{ summary | upper }}"+ deployment_body = "{{ summary | length }}"defaults.get("events").content_template = deployment_bodymonkeypatch.setattr("openviking.server.routers.admin.get_default_registry", lambda: defaults)url = f"/api/v1/admin/accounts/{account_id}/memory-templates/events"- assert (await lightweight_admin_client.put(url, json={}, headers=headers)).status_code == 200+ response = await lightweight_admin_client.put(+ url, json={"description": "Account instructions"}, headers=headers+ )+ assert response.status_code == 200, response.textfs = lightweight_admin_app.state.fake_service.viking_fspath = account_memory_template_path(account_id, "events")original = fs.agfs._files[path]@@ -731,26 +1063,39 @@ async def test_account_memory_templates_recheck_persisted_body_without_trusting_assert (await lightweight_admin_client.get(url, headers=headers)).status_code == 200[email protected]("restore_defaults", [False, True])async def test_account_memory_templates_recheck_trust_after_deployment_change(lightweight_admin_client,lightweight_admin_app,template_account,monkeypatch,+ restore_defaults,):from openviking_cli.exceptions import FailedPreconditionErroraccount_id, headers = template_accountdefaults = MemoryTypeRegistry()- old_body = "{{ summary | upper }}"+ old_body = "{{ summary | length }}"defaults.get("events").content_template = old_bodymonkeypatch.setattr("openviking.server.routers.admin.get_default_registry", lambda: defaults)url = f"/api/v1/admin/accounts/{account_id}/memory-templates/events"- assert (await lightweight_admin_client.put(url, json={}, headers=headers)).status_code == 200+ response = await lightweight_admin_client.put(+ url, json={"description": "Account instructions"}, headers=headers+ )+ assert response.status_code == 200, response.textfs = lightweight_admin_app.state.fake_service.viking_fsold_snapshot = await resolve_account_memory_registry(fs, account_id, defaults)+ if restore_defaults:+ response = await lightweight_admin_client.put(url, json={}, headers=headers)+ assert response.status_code == 200, response.text+ assert response.json()["result"]["status"] == "system_default"defaults.get("events").content_template = "# {{ summary }}"- with pytest.raises(FailedPreconditionError):- await resolve_account_memory_registry(fs, account_id, defaults)+ if restore_defaults:+ current = await resolve_account_memory_registry(fs, account_id, defaults)+ assert current.get("events").content_template == "# {{ summary }}"+ else:+ with pytest.raises(FailedPreconditionError):+ await resolve_account_memory_registry(fs, account_id, defaults)# Already-started extractions keep their snapshot, not a mutable defaults view.assert old_snapshot.get("events").content_template == old_bodyassert old_snapshot.get("events")._account_content_template is False@@ -774,9 +1119,12 @@ async def test_account_memory_templates_old_content_can_be_read_replaced_and_resfs = lightweight_admin_app.state.fake_service.viking_fsurl = f"/api/v1/admin/accounts/{account_id}/memory-templates/events"path = account_memory_template_path(account_id, "events")- assert (await lightweight_admin_client.put(url, json={}, headers=headers)).status_code == 200+ response = await lightweight_admin_client.put(+ url, json={"description": "Account instructions"}, headers=headers+ )+ assert response.status_code == 200, response.textlegacy = yaml.safe_load(fs.agfs._files[path])- legacy["content_template"] = "{{ summary | upper }}"+ legacy["content_template"] = "{{ summary | length }}"raw = yaml.safe_dump(legacy).encode()for operation in ("PUT", "DELETE"):fs.agfs._files[path] = raw@@ -957,8 +1305,12 @@ async def test_account_memory_templates_permissions_and_isolation(@pytest.mark.parametrize("output_format", ["python", "json"])@pytest.mark.parametrize("marker",- ["{{ language.upper() }}", "{% if language == 'en' %}EN{% else %}OTHER{% endif %}"],- ids=["safe-method", "safe-condition"],+ [+ "{{ language.upper() }}",+ "{% if language == 'en' %}EN{% else %}OTHER{% endif %}",+ "{{ language | trim | upper }}",+ ],+ ids=["safe-method", "safe-condition", "safe-filters"],)async def test_account_memory_templates_reach_live_prompts(lightweight_admin_client,diff --git a/tests/session/memory/test_account_descriptions.py b/tests/session/memory/test_account_descriptions.pyindex 8203b8da5..b04db2e51 100644--- a/tests/session/memory/test_account_descriptions.py+++ b/tests/session/memory/test_account_descriptions.py@@ -37,9 +37,10 @@ def test_generators_render_description_strings(generator_type, plain_expected, tassert generator._render_description(" plain\n") == plain_expectedassert generator._render_description(" {{ language.upper() }} ") == template_expected+ assert generator._render_description(" {{ language | trim | upper }} ") == template_expectedassert generator._render_description("") == ""with pytest.raises(DescriptionTemplateError):- generator._render_description("{{ language | upper }}")+ generator._render_description("{{ language | length }}")@pytest.mark.parametrize("memory_type", EDITABLE_MEMORY_TEMPLATE_FIELDS)@@ -54,6 +55,9 @@ def test_generators_render_description_strings(generator_type, plain_expected, t"English",),("{{ language.strip().upper().lower() }}", "en"),+ ("{{ language | trim | upper | lower }}", "en"),+ ("{% set label = language | upper %}{{ label }}", "EN"),+ ("{{ language | default('en') | upper }}", "EN"),("{% set label = language.upper() %}{{ label }}", "EN"),("{% for title, value in [('Use', language), ('Also', 'dates')] %}{{ loop.index }} {{ title }} {{ value }};{% endfor %}",@@ -131,7 +135,9 @@ def test_unchanged_deployment_descriptions_keep_existing_language_rendering(requ"{% for n in range(3) %}x{% endfor %}","{% include 'not_a_file' %}","literal {{ and {% unclosed",- "{{ language | upper }}",+ "{{ language | length }}",+ "{{ language | trim('x') }}",+ "{{ language | attr('__class__') }}","{{ summary }}","{{ extract_context.get_year(ranges) }}","{{ language[0] }}",@@ -203,7 +209,13 @@ def test_deployment_change_does_not_change_old_description_rendering():def test_missing_context_keeps_existing_undefined_behavior_and_does_not_recurse():assert render_description_template("{{ language }}", {}) == ""+ assert render_description_template("{{ language | default('en') }}", {}) == "en"+ assert render_description_template("{{ language | default('en') }}", {"language": ""}) == ""assert render_description_template("{{ language or 'unspecified' }}", {}) == "unspecified"+ assert (+ render_description_template("{{ (language or 'unspecified') | upper }}", {})+ == "UNSPECIFIED"+ )assert (render_description_template("{% if language is undefined %}missing{% endif %}", {})== "missing"diff --git a/tests/session/memory/test_account_template_locking.py b/tests/session/memory/test_account_template_locking.pyindex 303102fc0..723bd72b2 100644--- a/tests/session/memory/test_account_template_locking.py+++ b/tests/session/memory/test_account_template_locking.py@@ -12,6 +12,7 @@ import pytestfrom openviking.pyagfs import AGFSNotFoundErrorfrom openviking.session.memory.account_templates import (account_memory_template_path,+ default_memory_template,read_account_memory_template,resolve_account_memory_registry,update_account_memory_template,@@ -371,6 +372,54 @@ def test_publication_failures_never_overwrite_the_active_file(monkeypatch, existasyncio.run(run())[email protected]("failure", [None, "backup", "remove"])+def test_saving_defaults_uses_locked_reset_and_preserves_failed_writes(monkeypatch, failure):+ agfs = LockingAGFS()+ fs = SimpleNamespace(agfs=agfs)+ registry = MemoryTypeRegistry()+ path = account_memory_template_path("locking-test", "profile")++ async def run():+ await update_account_memory_template(+ fs, "locking-test", "profile", {"description": "Custom"}, registry+ )+ previous = agfs.files[path]+ write, remove = agfs.write, agfs.rm++ def checked_write(target, data, **kwargs):+ assert target == path + ".backup", "saving defaults must not publish another file"+ assert path in agfs.leases+ if failure == "backup":+ raise OSError("backup failed")+ return write(target, data, **kwargs)++ def checked_remove(target, **kwargs):+ assert target == path+ assert agfs.files[path + ".backup"] == previous+ if failure == "remove":+ raise OSError("remove failed")+ return remove(target, **kwargs)++ monkeypatch.setattr(agfs, "write", checked_write)+ monkeypatch.setattr(agfs, "rm", checked_remove)+ update = update_account_memory_template(+ fs, "locking-test", "profile", default_memory_template(registry, "profile"), registry+ )+ if failure:+ with pytest.raises(OSError, match=f"{failure} failed"):+ await update+ assert agfs.files[path] == previous+ else:+ assert await update is None+ assert path not in agfs.files+ assert agfs.files[path + ".backup"] == previous+ assert not agfs.leases+ assert all(not lock.locked() for lock in agfs.locks.values())+ assert not any(name.endswith(".tmp") for name in agfs.files)++ asyncio.run(run())++@pytest.mark.parametrize("workers", [None, 1], ids=["default-executor", "one-worker"])@pytest.mark.parametrize("encrypted", [False, True], ids=["plaintext", "encrypted"])def test_native_template_read_publish_and_reset(tmp_path, workers, encrypted):diff --git a/tests/session/memory/test_content_template.py b/tests/session/memory/test_content_template.pyindex 7b4dad5f7..29d3a4490 100644--- a/tests/session/memory/test_content_template.py+++ b/tests/session/memory/test_content_template.py@@ -2,7 +2,7 @@# SPDX-License-Identifier: AGPL-3.0from types import SimpleNamespace-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, Mockimport pytest@@ -87,6 +87,222 @@ def test_events_default_and_custom_render_real_context(resource_event):assert ("viking://resources/guide" in result) == resource_event[email protected]("memory_type", ["events", "soul", "identity"])[email protected]("edit", ["exact", "rstrip", "newline", "crlf", "heading"])[email protected]("message_kind", ["ordinary", "resource", "empty"])+def test_builtin_body_edits_publish_and_render(memory_type, edit, message_kind):+ defaults = memory_template_data(MemoryTypeRegistry().get(memory_type))+ original = defaults["content_template"]+ template = {+ "exact": original,+ "rstrip": original.rstrip(),+ "newline": original + "\n",+ "crlf": original.replace("\n", "\r\n"),+ "heading": "# Account memory\n" + original,+ }[edit]+ data = _complete_template(defaults, {"content_template": template}, memory_type)+ schema = _validate_template(data, memory_type, deployment_defaults=defaults)+ assert schema._account_content_template == (template != original)+ # The built-in syntax must pass the sandbox itself, not just inheritance.+ validate_content_template(template, memory_type)+ text = (+ "## Resource Addition\nResource URI: viking://resources/guide\n"+ if message_kind == "resource"+ else "We agreed to launch on Monday."+ )+ context = ExtractContext(+ []+ if message_kind == "empty"+ else [+ Message(+ id="m1", role="user", parts=[TextPart(text)], created_at="2026-09-07T08:00:00+00:00"+ )+ ]+ )+ values = {field.name: "Business fact" for field in schema.fields}+ values["ranges"] = "" if message_kind == "empty" else "0"+ expected = TemplateUtils.render(template, values, context)+ assert render_content_template(template, memory_type, values, context) == expected+ rendered = MemoryFileUtils.write(+ MemoryFile(memory_type=memory_type, extra_fields=values),+ content_template=schema.content_template,+ extract_context=context,+ account_content_template_type=memory_type if schema._account_content_template else None,+ )+ assert MemoryFileUtils.read(rendered).content == expected++[email protected]("expression", ["default", "default()", "default('N/A')"])[email protected]("value_kind", ["undefined", "none", "empty", "text"])+def test_default_filter_preserves_deployment_semantics(expression, value_kind):+ from jinja2 import Undefined++ value = {"undefined": Undefined(name="date"), "none": None, "empty": "", "text": "date"}[+ value_kind+ ]+ context = SimpleNamespace(get_first_message_time_from_ranges=lambda *args: value)+ template = (+ "Date: {{ extract_context.get_first_message_time_from_ranges(ranges) | "+ + expression+ + " }}"+ )+ assert render_content_template(template, "events", {"ranges": ""}, context) == (+ TemplateUtils.render(template, {"ranges": ""}, context)+ )+++def test_default_filter_rejects_untrusted_values_without_coercion():+ class Impostor:+ def __str__(self):+ pytest.fail("default must not coerce an untrusted object")++ def __bool__(self):+ pytest.fail("default must not evaluate untrusted truthiness")++ class StringSubclass(str):+ def __str__(self):+ pytest.fail("default must not coerce a string subclass")++ for value in (Impostor(), StringSubclass("text"), {}, 1, False):+ context = SimpleNamespace(get_event_content=lambda *args, result=value: result)+ with pytest.raises(ContentTemplateError, match="render_failed"):+ render_content_template(+ "{{ extract_context.get_event_content(ranges, summary) | default('fallback') }}",+ "events",+ {"ranges": "0"},+ context,+ )+++ "expression",+ [+ "ranges",+ "ranges|default",+ "ranges|default()",+ "ranges|default('')",+ "ranges|default('0-999')",+ "ranges|default('')|trim",+ "ranges.strip()",+ "selected",+ "selected|trim|upper|lower",+ "ranges if summary else ''",+ "summary",+ ],+)[email protected]("ranges", ["", "0", "0-3,7"])+def test_event_helper_accepts_equivalent_range_expressions(expression, ranges):+ get_year = Mock(spec=[], return_value="2026")+ template = "{% set selected = ranges %}{{ extract_context.get_year(" + expression + ") }}"+ validate_content_template(template, "events")+ assert (+ render_content_template(+ template,+ "events",+ {"ranges": ranges, "summary": ranges},+ SimpleNamespace(get_year=get_year),+ )+ == "2026"+ )+ get_year.assert_called_once_with(ranges)+++ "expression,ranges",+ [+ ("'0-999999999'", "0"),+ ("summary|default('')", "0"),+ ("selected", "0"),+ ("ranges or '0-999'", ""),+ ("ranges if not summary else summary", "0"),+ ("ranges|trim", " 0 "),+ ("none", "0"),+ ("false", "0"),+ ("1", "0"),+ ],+)+def test_event_helper_rejects_changed_or_nonstring_ranges_before_call(expression, ranges):+ get_year = Mock(spec=[], return_value="2026")+ template = "{% set selected = summary %}{{ extract_context.get_year(" + expression + ") }}"+ # Publication checks syntax; only extraction has the actual field values.+ validate_content_template(template, "events")+ with pytest.raises(ContentTemplateError, match="invalid_ranges"):+ render_content_template(+ template,+ "events",+ {"ranges": ranges, "summary": "0-999"},+ SimpleNamespace(get_year=get_year),+ )+ get_year.assert_not_called()+++def test_event_helper_checks_nested_call_results_without_object_coercion():+ class Impostor:+ def __eq__(self, other):+ pytest.fail("Range validation must not compare arbitrary objects")++ def __str__(self):+ pytest.fail("Range validation must not coerce arbitrary objects")++ def __bool__(self):+ pytest.fail("Range validation must not evaluate arbitrary truthiness")++ class StringSubclass(str):+ def __eq__(self, other):+ pytest.fail("Range validation must not compare string subclasses")++ template = "{{ extract_context.get_year(extract_context.get_event_content(ranges, summary)) }}"+ validate_content_template(template, "events")+ for value in (Impostor(), StringSubclass("0"), {}, [], 0, False, None):+ get_content = Mock(spec=[], return_value=value)+ get_year = Mock(spec=[], return_value="2026")+ with pytest.raises(ContentTemplateError, match="invalid_ranges"):+ render_content_template(+ template,+ "events",+ {"ranges": "0", "summary": "summary"},+ SimpleNamespace(get_event_content=get_content, get_year=get_year),+ )+ get_content.assert_called_once_with("0", "summary")+ get_year.assert_not_called()++[email protected]("flag", ["unsafe_callable", "alters_data"])+def test_event_helper_preserves_sandbox_callable_flags(flag):+ helper = Mock(spec=[], return_value="2026")+ setattr(helper, flag, True)+ with pytest.raises(ContentTemplateError, match="render_failed"):+ render_content_template(+ "{{ extract_context.get_year(ranges) }}",+ "events",+ {"ranges": "0"},+ SimpleNamespace(get_year=helper),+ )+ helper.assert_not_called()+++def test_event_helper_checks_each_call_against_current_render_ranges():+ helper = Mock(spec=[], return_value="2026")+ context = SimpleNamespace(get_year=helper)+ template = "{% for selected in [ranges, summary] %}{{ extract_context.get_year(selected) }}{% endfor %}"+ for ranges in ("0", "1"):+ helper.reset_mock()+ assert (+ render_content_template(+ template, "events", {"ranges": ranges, "summary": ranges}, context+ )+ == "20262026"+ )+ assert [call.args for call in helper.call_args_list] == [(ranges,), (ranges,)]+ helper.reset_mock()+ with pytest.raises(ContentTemplateError, match="invalid_ranges"):+ render_content_template(+ template, "events", {"ranges": ranges, "summary": "0-999"}, context+ )+ # Earlier valid reads do not exempt subsequent calls from validation.+ helper.assert_called_once_with(ranges)++def test_content_template_if_set_for_and_string_methods():template = """{% set heading = 'Business rules' %}# {{ heading }}@@ -117,10 +333,25 @@ def test_content_template_if_set_for_and_string_methods():"TITLE: AbC",),("{{ (summary or 'pending').upper() }}", "ABC"),+ ("{{ summary | trim }}", "AbC"),+ ("{{ summary | upper }}", "ABC"),+ ("{{ summary | lower }}", "abc"),+ ("{{ summary | trim | upper }}", "ABC"),+ ("{{ summary | default('pending') | trim | upper }}", "ABC"),+ ("{{ summary.strip() | lower }}", "abc"),+ ("{{ (summary | trim).upper() }}", "ABC"),+ ("{{ ' HeLLo ' | trim | lower }}", "hello"),+ ("{% set text = summary | trim %}{{ text | upper }}", "ABC"),+ ("{% if summary | trim | lower == 'abc' %}match{% endif %}", "match"),+ (+ "{% for title, text in [('Title', summary)] %}{{ title | upper }}: {{ text | trim }}{% endfor %}",+ "TITLE: AbC",+ ),],)-def test_content_template_string_methods_match_deployment_syntax(template, expected):+def test_content_template_string_formatting_matches_deployment_syntax(template, expected):fields = {"summary": " AbC "}+ validate_content_template(template, "events")assert render_content_template(template, "events", fields) == expectedassert TemplateUtils.render(template, fields) == expected@@ -139,17 +370,40 @@ def test_content_template_string_methods_on_helper_results_and_empty_fields():assert render_content_template("{{ summary.strip() or 'pending' }}", "events", {}) == "pending"+def test_content_template_filters_on_helper_results_and_empty_fields():+ context = SimpleNamespace(get_event_content=lambda *args: " details ")+ assert (+ render_content_template(+ "{{ extract_context.get_event_content(ranges, summary) | trim | upper }}",+ "events",+ {"ranges": "0"},+ context,+ )+ == "DETAILS"+ )+ assert render_content_template("{{ (summary | trim) or 'pending' }}", "events", {}) == "pending"++[email protected]("operation", [".upper()", " | upper", " | lower", " | trim"])@pytest.mark.parametrize("receiver_kind", ["object", "mapping", "str_subclass", "none", "int"])-def test_content_template_rejects_same_named_methods_before_attribute_lookup(receiver_kind):+def test_content_template_rejects_untrusted_string_receivers(receiver_kind, operation):touched = []class Impostor:+ def __str__(self):+ touched.append("string conversion")+ return "unsafe"+@propertydef upper(self):touched.append("property")return lambda: "unsafe"class StringSubclass(str):+ def __str__(self):+ touched.append("subclass conversion")+ return "unsafe"+def upper(self):touched.append("override")return "unsafe"@@ -164,7 +418,7 @@ def test_content_template_rejects_same_named_methods_before_attribute_lookup(reccontext = SimpleNamespace(get_event_content=lambda *args: receivers[receiver_kind])with pytest.raises(ContentTemplateError, match="render_failed"):render_content_template(- "{{ extract_context.get_event_content(ranges, summary).upper() }}",+ "{{ extract_context.get_event_content(ranges, summary)" + operation + " }}","events",{"ranges": "0"},context,@@ -175,15 +429,14 @@ def test_content_template_rejects_same_named_methods_before_attribute_lookup(rec@pytest.mark.parametrize("expression",[- "summary | upper",- "summary | lower",- "summary | trim","summary | length",- "summary | default('pending', true)",- "summary.upper() | upper",+ "summary | d('pending')",+ "summary | replace('a', 'b')",+ "summary | safe",+ "summary | unknown",],)-def test_content_template_rejects_all_filter_syntax(expression):+def test_content_template_rejects_unapproved_filters(expression):template = "{{ " + expression + " }}"with pytest.raises(ContentTemplateError, match="unsupported_filter"):validate_content_template(template, "events")@@ -191,6 +444,31 @@ def test_content_template_rejects_all_filter_syntax(expression):render_content_template(template, "events", {"summary": "text"})+ "expression",+ [+ "summary | trim('x')",+ "summary | trim(chars='x')",+ "summary | upper(1)",+ "summary | lower(*summary)",+ "summary | trim(**summary)",+ "summary | default('pending', true)",+ "summary | default(default_value='pending')",+ "summary | default(summary)",+ "summary | default(1)",+ "summary | default(none)",+ "summary | default(*summary)",+ "summary | default(**summary)",+ ],+)+def test_content_template_rejects_filter_arguments(expression):+ template = "{{ " + expression + " }}"+ with pytest.raises(ContentTemplateError, match="invalid_arguments"):+ validate_content_template(template, "events")+ with pytest.raises(ContentTemplateError, match="invalid_arguments"):+ render_content_template(template, "events", {"summary": "text"})++def test_events_builtin_template_keeps_original_date_fallback():template = MemoryTypeRegistry().get("events").content_templateassert "ranges|default('')" in template@@ -224,6 +502,9 @@ def test_events_builtin_template_keeps_original_date_fallback():"{{ summary.replace('a', 'b') }}","{{ summary.format() }}","{{ summary.strip().__class__ }}",+ "{{ (summary | trim).__class__ }}",+ "{{ summary | trim | attr('__class__') }}",+ "{% filter trim %}text{% endfilter %}","{{ summary.upper.__self__ }}","{{ extract_context.upper() }}","{% for x in [1] %}{{ loop.upper() }}{% endfor %}",@@ -243,7 +524,6 @@ def test_events_builtin_template_keeps_original_date_fallback():"{% for x in [1] recursive %}x{% endfor %}","{% set x = [summary, summary] %}{{ x }}","{{ extract_context.get_event_content() }}",- "{{ extract_context.get_year('0-999999999') }}","{{ extract_context.get_year(ranges|default('0-999', true)) }}","{{ extract_context.get_year(ranges_str=ranges) }}","{{ extract_context.get_event_content(ranges, summary, 2) }}",@@ -300,7 +580,8 @@ def test_content_template_cannot_override_hidden_metadata():@pytest.mark.asyncio-async def test_account_content_template_runtime_failure_preserves_existing_file():[email protected]("changed_ranges", [False, True])+async def test_account_content_template_runtime_failure_preserves_existing_file(changed_ranges):from openviking.server.identity import RequestContext, Rolefrom openviking.session.memory.dataclass import ResolvedOperationfrom openviking.session.memory.memory_updater import MemoryUpdater@@ -308,8 +589,12 @@ async def test_account_content_template_runtime_failure_preserves_existing_file(registry = MemoryTypeRegistry()schema = registry.get("events")- # Valid template, but the extraction context is unavailable at write time.- schema.content_template = "{{ extract_context.get_year(ranges) }}"+ # Valid syntax can still fail on unavailable context or changed ranges.+ schema.content_template = (+ "{% set selected = summary %}{{ extract_context.get_year(selected) }}"+ if changed_ranges+ else "{{ extract_context.get_year(ranges) }}"+ )schema._account_content_template = Truevalidate_content_template(schema.content_template, "events")fs = SimpleNamespace(@@ -317,16 +602,21 @@ async def test_account_content_template_runtime_failure_preserves_existing_file()updater = MemoryUpdater(registry=registry)updater._viking_fs = fs- with pytest.raises(ContentTemplateError, match="render_failed"):+ get_year = Mock(spec=[], return_value="2026")+ with pytest.raises(+ ContentTemplateError, match="invalid_ranges" if changed_ranges else "render_failed"+ ):await updater._apply_upsert(ResolvedOperation(memory_type="events",uris=["viking://user/alice/memories/events/meeting.md"],- memory_fields={},+ memory_fields={"ranges": "0", "summary": "0-999"},),RequestContext(user=UserIdentifier("space_a", "alice"), role=Role.USER),+ extract_context=SimpleNamespace(get_year=get_year) if changed_ranges else None,)fs.write_file.assert_not_awaited()+ get_year.assert_not_called()@pytest.mark.asyncio@@ -374,14 +664,34 @@ async def test_account_content_template_initialization(monkeypatch):("get_day", "ranges"),],)-def test_content_template_all_documented_helpers(method, args):- context = SimpleNamespace(**{method: lambda *args: "ok"})[email protected]("expression", ["ranges", "selected", "ranges|default('')|trim", "''"])+def test_content_template_all_documented_helpers(method, args, expression):+ helper = Mock(spec=[], return_value="ok")+ context = SimpleNamespace(**{method: helper})+ prelude = "{% set selected = ranges %}"+ actual_args = args.replace("ranges", expression, 1)assert (render_content_template(- "{{ extract_context." + method + "(" + args + ") }}",+ prelude + "{{ extract_context." + method + "(" + actual_args + ") }}","events",{"ranges": "0", "summary": "summary"},context,)== "ok")+ expected_args = ("" if expression == "''" else "0",)+ if "summary" in args:+ expected_args += ("summary",)+ if args.endswith(", 0"):+ expected_args += (0,)+ helper.assert_called_once_with(*expected_args)+ helper.reset_mock()+ changed_args = args.replace("ranges", "'0-999'", 1)+ with pytest.raises(ContentTemplateError, match="invalid_ranges"):+ render_content_template(+ "{{ extract_context." + method + "(" + changed_args + ") }}",+ "events",+ {"ranges": "0", "summary": "summary"},+ context,+ )+ helper.assert_not_called()