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: watch task status (#4589)
zihengli94a10313bf13
diff --git a/docs/en/api/02-resources.md b/docs/en/api/02-resources.mdindex c7a67920c..3acffa173 100644--- a/docs/en/api/02-resources.md+++ b/docs/en/api/02-resources.md@@ -179,6 +179,7 @@ This endpoint is the core entry point for resource management, supporting adding| preserve_structure | bool | No | None | Whether to preserve directory structure || args | object | No | `{}` | Parser-specific import options forwarded to the source parser/accessor. Native HTTPS Git imports and watches accept HTTP Basic credentials over TLS as `args.auth_config={"username":"oauth2","token":"..."}`; `username` defaults to `oauth2`. Git `branch` or `commit` remains at the top level of `args`. To import a private TOS object through its HTTP(S) URL, pass exactly one non-empty string: `args.tos_signature` (sent as `X-Tos-Signature`) or `args.tos_access` (sent as `X-Tos-Access`). TOS credentials are used only for the current HEAD/GET fetch, which is staged as a snapshot; they are not persisted to resource metadata or queue jobs. `args.parse_mode` accepts `default` (existing splitting behavior) or `no_split` (parse and convert each source document to one Markdown body). E.g. `args.site=true/false` forces/opts out of whole-site (sitemap/RSS) ingestion, `args.max_pages` etc. override the `webfeed` config; the recursive web crawler accepts `args.depth`, `args.max_pages`, `args.include_paths`, `args.exclude_paths`, `args.allow_external_links`, `args.skip_download_links`; Feishu user-token imports pass `args.feishu_access_token`. Core `add_resource` fields such as `path`, `to`, `watch_interval`, `include`, and `exclude` are not allowed inside `args` || watch_interval | float | No | 0 | Scheduled update interval (minutes). >0 creates a task for a re-readable URL/sitemap/RSS source; uploaded `temp_file_id` content is a static snapshot and must be re-added when it changes. <=0 cancels a task; explicit `to` wins, otherwise binds to the imported `root_uri` |+| is_active | bool | No | True | Initial Watch scheduling state. `false` requires `watch_interval > 0` and either `to` or `parent`. `parent` is supported for native Feishu URL imports; Connector imports still require an exact `to`. The initial import still runs once and the Watch remains paused afterward || processing_mode | string | No | `semantic_and_vectors` | Post-ingest processing mode. `semantic_and_vectors` is the normal flow: generate semantic artifacts (`.abstract.md`, `.overview.md`) and vectors. `vectors_only` skips semantic understanding/VLM summarization and only vectorizes current resource files || telemetry | TelemetryRequest | No | False | Whether to return telemetry data |@@ -200,6 +201,7 @@ This endpoint is the core entry point for resource management, supporting adding- `processing_mode=vectors_only` does not call the VLM semantic-understanding stage and does not generate or refresh `.abstract.md` / `.overview.md`. For existing targets, it preserves existing semantic artifacts and existing semantic vectors. It still updates the resource tree, vectorizes current non-hidden files when `build_index=true`, and removes detail vectors for files deleted during refresh.- `processing_mode` belongs to `add_resource`. The admin `reindex` API/CLI continues to use `mode` (`vectors_only`, `semantic_and_vectors`, `prune_orphans`) for maintenance operations on already-ingested data.- When `watch_interval > 0`, the watch task binds to `to` if provided; otherwise it binds to the `root_uri` returned by this import. If no stable `root_uri` is available, the request fails and asks for an explicit `to`.+- For Connector imports, `is_active=false` creates the paused Watch before submission. Native Feishu imports carry `is_active` through the resource queue and create the Watch after resolving the imported resource URI. In both cases, the initial import still runs once and periodic scheduling remains disabled.- Feishu/Lark app-token imports do not pass `args.feishu_access_token`. OpenViking keeps the existing app credential flow and the SDK obtains an app/tenant token from `app_id` and `app_secret`. This mode supports both one-time imports and `watch_interval > 0`.- Feishu/Lark one-time user-token imports pass `args={"feishu_access_token": "u-..."}` with `watch_interval <= 0`. OpenViking uses that user token only for the current import and does not store it.- Feishu/Lark user-token watches pass `args={"feishu_access_token": "u-...", "feishu_refresh_token": "r-..."}` with `watch_interval > 0`. They may also pass `feishu_app_id` and `feishu_app_secret` together; OpenViking stores the pair in the private watch task state and uses it to refresh that watch's user token.diff --git a/docs/en/api/15-watches.md b/docs/en/api/15-watches.mdindex b15f16a29..1c87845cd 100644--- a/docs/en/api/15-watches.md+++ b/docs/en/api/15-watches.md@@ -151,6 +151,9 @@ Listing tasks returns:"processor_kwargs": {},"created_at": "2026-07-24T10:00:00","last_execution_time": null,+ "last_task_id": null,+ "last_status": null,+ "last_error": null,"next_execution_time": "2026-07-24T10:30:00","is_active": true,"account_id": "default",@@ -167,6 +170,12 @@ Listing tasks returns:precedence (for example, `tos` or `feishu_project`); native imports report `feishu`, `git`,`url`, or `local`. Legacy or unclassified watches return `null`.+Before the first run, `last_task_id`, `last_status`, and `last_error` are `null`. After a run,+`last_task_id` points to the corresponding ingestion task (and can remain `null` for preflight+failures), while `last_status` is `completed`, `failed`, or `cancelled`. On failure,+`last_error` contains a credential-redacted error limited to 500 characters. Use+`ov task status <last_task_id>` for the ingestion task details.+Getting one task and a successful update return the same task object directly in `result`. Delete and trigger return:```json@@ -191,7 +200,9 @@ Getting one task and a successful update return the same task object directly in}```-`scheduled=true` only confirms that background execution was scheduled. It does not mean re-ingestion has completed; read the task again and inspect `last_execution_time`.+`scheduled=true` only confirms that background execution was scheduled. It does not mean+re-ingestion has completed; read the task again until `last_execution_time` changes, then inspect+`last_status` and `last_error`.**MCP** (agent control plane — minimum closure only)diff --git a/docs/zh/api/02-resources.md b/docs/zh/api/02-resources.mdindex 2a3561e73..4f037c1fb 100644--- a/docs/zh/api/02-resources.md+++ b/docs/zh/api/02-resources.md@@ -172,6 +172,7 @@ URL/文件 Parser TreeBuilder AGFS Summarizer/Vector| preserve_structure | bool | 否 | None | 是否保留目录结构 || args | object | 否 | `{}` | 传给特定 parser/accessor 的导入参数。原生 HTTPS Git 导入和 Watch 可通过 `args.auth_config={"username":"oauth2","token":"..."}` 在 TLS 上传递 HTTP Basic 凭据;`username` 默认为 `oauth2`。Git 的 `branch` 或 `commit` 仍放在 `args` 顶层。通过 HTTP(S) URL 导入私有 TOS 对象时,二选一传入非空字符串:`args.tos_signature`(映射为 `X-Tos-Signature`)或 `args.tos_access`(映射为 `X-Tos-Access`)。TOS 凭证只用于当前 HEAD/GET 抓取;资源会先保存为快照,凭证不会写入资源元数据或队列任务。`args.parse_mode` 支持 `default`(保持现有拆分行为)和 `no_split`(正常解析并将每个源文档正文保存为一个 Markdown 文件)。例如 `args.site=true/false` 强制/禁用整站(sitemap/RSS)导入,`args.max_pages` 等可覆盖 `webfeed` 配置;递归网页爬虫支持 `args.depth`、`args.max_pages`、`args.include_paths`、`args.exclude_paths`、`args.allow_external_links`、`args.skip_download_links`;飞书用户 token 导入传 `args.feishu_access_token`。`path`、`to`、`watch_interval`、`include`、`exclude` 等 `add_resource` 核心字段不能放入 `args` || watch_interval | float | 否 | 0 | 定时更新间隔(分钟)。>0 为 URL/sitemap/RSS 等可重新读取的来源创建任务;通过 `temp_file_id` 上传的内容是一次性快照,变化后需重新添加。≤0 取消任务;显式 `to` 优先,否则绑定本次导入的 `root_uri` |+| is_active | bool | 否 | True | Watch 初始调度状态。设为 `false` 时要求 `watch_interval > 0`,并在 `to`、`parent` 中二选一。`parent` 仅支持原生飞书 URL 导入;Connector 仍要求精确的 `to`。首次导入仍执行一次,随后保持暂停 || processing_mode | string | 否 | `semantic_and_vectors` | 入库后的处理模式。`semantic_and_vectors` 是默认流程:生成语义产物(`.abstract.md`、`.overview.md`)并生成向量。`vectors_only` 跳过语义理解/VLM 总结,只对当前资源文件生成向量 || tags | string[] | 否 | None | 导入时写入向量检索记录的显式检索标签,格式必须是 `k=v`,例如 `["team=search", "env=test"]`。搜索接口可用同名 `tags` 参数过滤召回 || tag_mode | string | 否 | `"replace"` | `tags` 的写入模式,可选 `replace` 或 `append`。导入新资源时会随本次生成的每条向量记录写入;不会在完成后额外调用 `set_tags`,响应也不返回 `tags_result` |@@ -195,6 +196,7 @@ URL/文件 Parser TreeBuilder AGFS Summarizer/Vector- `processing_mode=vectors_only` 不调用 VLM 语义理解阶段,也不会生成或刷新 `.abstract.md` / `.overview.md`。对已存在目标,它会保留旧的语义产物和旧的语义向量;仍会更新资源树,在 `build_index=true` 时向量化当前非隐藏文件,并清理由本次刷新删除的文件 detail 向量。- `processing_mode` 只属于 `add_resource`。管理员维护已有数据时,`reindex` API/CLI 仍使用 `mode`(`vectors_only`、`semantic_and_vectors`、`prune_orphans`)。- `watch_interval > 0` 时,如果指定了 `to`,监控任务绑定该目标;如果未指定 `to`,监控任务绑定本次导入返回的 `root_uri`。如果无法得到稳定 `root_uri`,请求会报错并要求显式传 `to`。+- Connector 导入设置 `is_active=false` 时会在提交前创建暂停状态的 Watch;原生飞书导入会通过资源队列透传 `is_active`,解析出最终资源 URI 后再创建 Watch。两种情况下首次导入均执行一次,周期调度保持关闭。- 飞书/Lark 应用 token 导入不传 `args.feishu_access_token`。OpenViking 保持原有应用凭证流程,由 SDK 使用 `app_id` 和 `app_secret` 自动获取 app/tenant token。该模式支持一次性导入和 `watch_interval > 0`。- 飞书/Lark 一次性用户 token 导入通过 `args={"feishu_access_token": "u-..."}` 传入,且 `watch_interval <= 0`。OpenViking 只在本次导入使用该用户 token,不保存。- 飞书/Lark 用户 token watch 通过 `args={"feishu_access_token": "u-...", "feishu_refresh_token": "r-..."}` 传入,且 `watch_interval > 0`。还可同时传入 `feishu_app_id` 和 `feishu_app_secret`;OpenViking 会将其保存在 watch task 私有状态中,并用于刷新该 watch 的用户 token。diff --git a/docs/zh/api/15-watches.md b/docs/zh/api/15-watches.mdindex 5b484ee6f..3f0abd695 100644--- a/docs/zh/api/15-watches.md+++ b/docs/zh/api/15-watches.md@@ -151,6 +151,9 @@ ov task watch rm viking://resources/guide.md"processor_kwargs": {},"created_at": "2026-07-24T10:00:00","last_execution_time": null,+ "last_task_id": null,+ "last_status": null,+ "last_error": null,"next_execution_time": "2026-07-24T10:30:00","is_active": true,"account_id": "default",@@ -167,6 +170,11 @@ ov task watch rm viking://resources/guide.md`feishu_project`);原生导入返回 `feishu`、`git`、`url` 或 `local`。历史任务或无法分类的任务返回 `null`。+首次执行前,`last_task_id`、`last_status` 和 `last_error` 均为 `null`。执行后,+`last_task_id` 指向对应的普通导入任务(预检查失败时可能为 `null`),`last_status`+为 `completed`、`failed` 或 `cancelled`;失败时 `last_error` 返回经过凭证脱敏且最多+500 字符的错误信息。可用 `ov task status <last_task_id>` 查看对应导入任务详情。+查看单个任务以及成功更新时,`result` 直接是同一结构的任务对象。删除和触发分别返回:```json@@ -191,7 +199,8 @@ ov task watch rm viking://resources/guide.md}```-`scheduled=true` 只表示后台执行已调度,不表示重新摄取已经完成;应再次查看任务的 `last_execution_time`。+`scheduled=true` 只表示后台执行已调度,不表示重新摄取已经完成;应再次查看任务,直到+`last_execution_time` 更新,并检查 `last_status` 和 `last_error`。**MCP**(Agent 控制面——仅最小闭包)diff --git a/openviking/connector/client.py b/openviking/connector/client.pyindex 7da1a566b..375d70d22 100644--- a/openviking/connector/client.py+++ b/openviking/connector/client.py@@ -64,6 +64,7 @@ class ConnectorClient:include_child: bool = True,param_config: Optional[Dict[str, Any]] = None,auth_config: Optional[Dict[str, Any]] = None,+ stream_states: Optional[Dict[str, Any]] = None,extra_params: Optional[Dict[str, Any]] = None,) -> Dict[str, Any]:"""Submit a document import job via the configured doc/add endpoint.@@ -89,10 +90,23 @@ class ConnectorClient:payload["param_config"] = param_configif auth_config:payload["auth_config"] = auth_config+ if stream_states is not None:+ payload["stream_states"] = stream_statesif extra_params:# Authentication belongs exclusively in the Authorization header.payload.update({key: value for key, value in extra_params.items() if key != "api_key"})+ # Log shape only: param_config values and auth_config may carry source credentials.+ logger.info(+ "[ConnectorClient] doc/add: add_type=%s to=%s include_child=%s param_config_keys=%s "+ "has_auth_config=%s has_stream_states=%s",+ add_type,+ to,+ include_child,+ sorted(param_config or {}),+ bool(auth_config),+ stream_states is not None,+ )async with httpx.AsyncClient(timeout=30.0) as client:rsp = await client.post(self._doc_add_url,@@ -120,4 +134,8 @@ class ConnectorClient:return dataif not isinstance(task, dict):raise InternalError("Connector task response contains an invalid Task object")+ task = dict(task)+ for key in ("StreamStates", "stream_states"):+ if key in data:+ task[key] = data[key]return taskdiff --git a/openviking/connector/delegate.py b/openviking/connector/delegate.pyindex e70dab7a5..5fa40760e 100644--- a/openviking/connector/delegate.py+++ b/openviking/connector/delegate.py@@ -35,7 +35,13 @@ from openviking.connector.routing import (detect_connector_add_type,is_full_commit_sha,)+from openviking.core.namespace import NamespaceShapeError+from openviking.core.uri_validation import matches_content_kindfrom openviking.crypto.encryptor import MAGIC as ENCRYPTED_ENVELOPE_MAGIC+from openviking.observability.http_error_context import (+ sanitize_public_error_details,+ sanitize_public_http_error,+)from openviking.parse.mode import ParseModefrom openviking.resource.processing_mode import (DEFAULT_PROCESSING_MODE,@@ -52,6 +58,17 @@ logger = get_logger(__name__)_TOS_BUCKET_PATTERN = re.compile(r"^[a-z0-9](?:[a-z0-9-]{1,61}[a-z0-9])$")+def _safe_http_response(response: httpx.Response) -> Any:+ """Return a bounded, credential-redacted response body for logs."""+ try:+ payload = response.json()+ except (TypeError, ValueError):+ payload = response.text+ if isinstance(payload, dict):+ return sanitize_public_error_details(payload)+ return sanitize_public_http_error(code="CONNECTOR_HTTP_ERROR", message=payload).message++def _validate_tos_uri(value: Any,field: str,@@ -94,6 +111,14 @@ def _validate_tos_uri(raise error+def _is_resource_target(to: str) -> bool:+ """True when *to* lies in a resources tree: the public root or a user's own."""+ try:+ return matches_content_kind(to, "resource")+ except (ValueError, NamespaceShapeError):+ return False++class ConnectorDelegate:"""Routes add_resource requests to the external Connector service.@@ -409,8 +434,11 @@ class ConnectorDelegate:unsupported.append("parent targets (Connector imports require an exact 'to' target)")if not to:unsupported.append("missing exact 'to' target")- elif to != "viking://resources" and not to.startswith("viking://resources/"):- unsupported.append("to outside the public resources root (viking://resources/...)")+ elif not _is_resource_target(to):+ unsupported.append(+ "to outside a resources tree (viking://resources/... or "+ "viking://user/<user_id>/resources/...)"+ )if instruction:unsupported.append("instruction")if not build_index:@@ -466,7 +494,11 @@ class ConnectorDelegate:tags: Optional[List[str]] = None,tag_mode: str = "replace",wait_for_completion: bool = False,- on_success: Optional[Callable[[], Awaitable[None]]] = None,+ connector_states: Optional[Dict[str, Any]] = None,+ on_success: Optional[Callable[[Optional[Dict[str, Any]]], Awaitable[None]]] = None,+ on_complete: Optional[+ Callable[[str, Optional[str], Optional[str]], Awaitable[None]]+ ] = None,**kwargs: Any,) -> Dict[str, Any]:"""Route add_resource to the external Connector service."""@@ -610,15 +642,31 @@ class ConnectorDelegate:user_id=ctx.user.user_id,)try:- result = await client.submit_doc_add(- add_type=add_type,- api_key=ctx.api_key,- tos_path=tos_path,- to=task_resource_id,- include_child=True,- param_config=param_config,- auth_config=auth_config or None,- extra_params=extra_params,+ submit_kwargs: Dict[str, Any] = {+ "add_type": add_type,+ "api_key": ctx.api_key,+ "tos_path": tos_path,+ "to": task_resource_id,+ "include_child": True,+ "param_config": param_config,+ "auth_config": auth_config or None,+ "extra_params": extra_params,+ }+ if connector_states is not None:+ submit_kwargs["stream_states"] = connector_states+ safe_submit_request = dict(submit_kwargs)+ if safe_submit_request.get("auth_config") is not None:+ safe_submit_request["auth_config"] = "[REDACTED]"+ logger.info(+ "[ConnectorDelegate] Connector task add request: ov_task_id=%s request=%s",+ task.task_id,+ sanitize_public_error_details(safe_submit_request),+ )+ result = await client.submit_doc_add(**submit_kwargs)+ logger.info(+ "[ConnectorDelegate] Connector task add response: ov_task_id=%s response=%s",+ task.task_id,+ sanitize_public_error_details(result),)connector_task_key = result.get("task_key") or result.get("TaskKey") or ""@@ -626,6 +674,19 @@ class ConnectorDelegate:raise InternalError(f"Connector accepted the import but returned no task key: {result}")+ # Never log api_key / auth_config / param_config values here.+ logger.info(+ "[ConnectorDelegate] Connector import accepted: add_type=%s to=%s "+ "ov_task_id=%s connector_task_key=%s account_id=%s has_auth_config=%s "+ "incremental=%s",+ add_type,+ task_resource_id,+ task.task_id,+ connector_task_key,+ ctx.account_id,+ bool(auth_config),+ connector_states is not None,+ )except asyncio.CancelledError:await task_tracker.fail(task.task_id,@@ -635,25 +696,65 @@ class ConnectorDelegate:)raiseexcept Exception as exc:+ if isinstance(exc, httpx.HTTPStatusError):+ logger.error(+ "[ConnectorDelegate] Connector task add response: ov_task_id=%s status=%s "+ "response=%s",+ task.task_id,+ exc.response.status_code,+ _safe_http_response(exc.response),+ )+ safe_error = sanitize_public_http_error(+ code="CONNECTOR_SUBMISSION_FAILED",+ message=exc,+ ).message+ logger.error(+ "[ConnectorDelegate] Connector submission failed: add_type=%s to=%s "+ "ov_task_id=%s error_type=%s error=%s",+ add_type,+ task_resource_id,+ task.task_id,+ type(exc).__name__,+ safe_error,+ )await task_tracker.fail(task.task_id,- str(exc),+ safe_error,account_id=ctx.account_id,user_id=ctx.user.user_id,)raise- monitor = self._monitor(- client=client,- connector_task_key=connector_task_key,- ov_task_id=task.task_id,- poll_interval_ms=config.poll_interval_ms,- timeout_seconds=config.timeout_seconds,- ctx=ctx,- reason=reason,- link_root_uri=task_resource_id or "viking://resources",- on_success=on_success,- )+ async def run_monitor() -> Dict[str, Any]:+ try:+ outcome = await self._monitor(+ client=client,+ connector_task_key=connector_task_key,+ ov_task_id=task.task_id,+ poll_interval_ms=config.poll_interval_ms,+ timeout_seconds=config.timeout_seconds,+ ctx=ctx,+ reason=reason,+ link_root_uri=task_resource_id or "viking://resources",+ on_success=on_success,+ )+ except asyncio.CancelledError:+ if on_complete is not None:+ await on_complete(+ "failed",+ task.task_id,+ "background connector task monitoring cancelled",+ )+ raise+ if on_complete is not None:+ await on_complete(+ str(outcome.get("status") or "failed"),+ task.task_id,+ outcome.get("error"),+ )+ return outcome++ monitor = run_monitor()response = {"status": "accepted",@@ -681,7 +782,7 @@ class ConnectorDelegate:ctx: RequestContext,reason: str = "",link_root_uri: str = "",- on_success: Optional[Callable[[], Awaitable[None]]] = None,+ on_success: Optional[Callable[[Optional[Dict[str, Any]]], Awaitable[None]]] = None,) -> Dict[str, Any]:"""Poll the Connector task until terminal state, then update OV TaskRecord.@@ -701,8 +802,10 @@ class ConnectorDelegate:)poll_interval = poll_interval_ms / 1000.0- deadline = time.perf_counter() + timeout_seconds+ started = time.perf_counter()+ deadline = started + timeout_secondsterminal_statuses = {"succeeded", "failed", "cancelled"}+ last_status = ""try:while time.perf_counter() < deadline:@@ -711,12 +814,18 @@ class ConnectorDelegate:info = await client.get_task_info(connector_task_key, ctx.api_key)except httpx.HTTPStatusError as exc:status_code = exc.response.status_code- if status_code not in {408, 429} and status_code < 500:- raise+ retrying = status_code in {408, 429} or status_code >= 500logger.warning(- "[ConnectorDelegate] Transient Connector task polling HTTP error "- f"for {connector_task_key}: {status_code}; retrying"+ "[ConnectorDelegate] Connector task info error response: "+ "connector_task_key=%s ov_task_id=%s status=%s response=%s retrying=%s",+ connector_task_key,+ ov_task_id,+ status_code,+ _safe_http_response(exc.response),+ retrying,)+ if not retrying:+ raisecontinueexcept httpx.RequestError:logger.warning(@@ -725,6 +834,15 @@ class ConnectorDelegate:)continuestatus = (info.get("Status") or info.get("status") or "").lower()+ if status != last_status:+ logger.info(+ "[ConnectorDelegate] Connector task %s: %s -> %s (ov_task_id=%s)",+ connector_task_key,+ last_status or "submitted",+ status,+ ov_task_id,+ )+ last_status = statusawait task_tracker.update_stage(ov_task_id,@@ -739,8 +857,17 @@ class ConnectorDelegate:"connector_status": status,"connector_task_key": connector_task_key,}+ connector_states = info.get("StreamStates")+ if connector_states is None:+ connector_states = info.get("stream_states")+ if connector_states is not None:+ if not isinstance(connector_states, dict):+ raise InternalError(+ "Connector task response contains invalid stream states"+ )+ completion["connector_states"] = connector_statesif on_success is not None:- await on_success()+ await on_success(connector_states)if (reason or "").strip() and link_root_uri:link_result: Dict[str, Any] = {"root_uri": link_root_uri}await self._link_reason_memory(@@ -753,6 +880,14 @@ class ConnectorDelegate:for key in ("memory_linking", "warnings"):if key in link_result:completion[key] = link_result[key]+ logger.info(+ "[ConnectorDelegate] Connector task %s succeeded after %.0fs "+ "(ov_task_id=%s, has_states=%s)",+ connector_task_key,+ time.perf_counter() - started,+ ov_task_id,+ connector_states is not None,+ )await task_tracker.complete(ov_task_id,completion,@@ -761,16 +896,48 @@ class ConnectorDelegate:)return {"status": "completed", **completion}error_msg = info.get("ErrorMessage") or info.get("error_message") or status- failure = f"connector task {status}: {error_msg}"- await task_tracker.fail(+ safe_error = sanitize_public_http_error(+ code="CONNECTOR_TASK_FAILED",+ message=error_msg,+ ).message+ failure = f"connector task {status}: {safe_error}"+ logger.warning(+ "[ConnectorDelegate] Connector task %s ended %s after %.0fs "+ "(ov_task_id=%s): %s response=%s",+ connector_task_key,+ status,+ time.perf_counter() - started,ov_task_id,- failure,- account_id=ctx.account_id,- user_id=ctx.user.user_id,+ safe_error,+ sanitize_public_error_details(info),)- return {"status": "failed", "error": failure}+ if status == "cancelled":+ await task_tracker.mark_cancelled(+ ov_task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ else:+ await task_tracker.fail(+ ov_task_id,+ failure,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ )+ return {+ "status": "cancelled" if status == "cancelled" else "failed",+ "error": failure,+ }timeout_msg = f"connector task timed out after {timeout_seconds}s"+ logger.warning(+ "[ConnectorDelegate] Connector task %s timed out after %ss (ov_task_id=%s, "+ "last_status=%s)",+ connector_task_key,+ timeout_seconds,+ ov_task_id,+ last_status or "submitted",+ )await task_tracker.fail(ov_task_id,timeout_msg,diff --git a/openviking/core/uri_validation.py b/openviking/core/uri_validation.pyindex 5d977e51d..5267fa259 100644--- a/openviking/core/uri_validation.py+++ b/openviking/core/uri_validation.py@@ -99,7 +99,7 @@ def validate_content_target_uri(canonical_uri = validate_request_viking_uri(raw_uri, ctx, field_name=field_name)- if _matches_content_kind(canonical_uri, kind):+ if matches_content_kind(canonical_uri, kind):if is_accessible(canonical_uri, ctx):return canonical_uriraise PermissionDeniedError(f"Access denied for {canonical_uri}", resource=canonical_uri)@@ -107,7 +107,7 @@ def validate_content_target_uri(raise InvalidURIError(raw_uri, f"{field_name} must target {kind} content")-def _matches_content_kind(uri: str, kind: str) -> bool:+def matches_content_kind(uri: str, kind: str) -> bool:if kind not in {"resource", "skill"}:raise ValueError(f"Unsupported content target kind: {kind}")parts = uri_parts(uri)diff --git a/openviking/observability/http_error_context.py b/openviking/observability/http_error_context.pyindex 4f02f8ada..03b4aa137 100644--- a/openviking/observability/http_error_context.py+++ b/openviking/observability/http_error_context.py@@ -25,6 +25,7 @@ _SENSITIVE_KEYS = frozenset({"access_token","api_key",+ "app_secret","authorization","client_secret","cookie",@@ -41,7 +42,7 @@ _SENSITIVE_KEYS = frozenset(})_SENSITIVE_ASSIGNMENT_RE = re.compile(- r"(?i)\b(access[_-]?token|api[_-]?key|authorization|client[_-]?secret|cookie|"+ r"(?i)\b(access[_-]?token|api[_-]?key|app[_-]?secret|authorization|client[_-]?secret|cookie|"r"credential|password|refresh[_-]?token|root[_-]?api[_-]?key|secret|"r"secret[_-]?access[_-]?key|secret[_-]?key|set[_-]?cookie|token|user[_-]?key)"r"(\s*[:=]\s*)(?:\"[^\"\r\n]*\"|'[^'\r\n]*'|[^\r\n,}\]]+)"@@ -172,7 +173,15 @@ def _redact_sensitive_text(value: str) -> str:def _is_sensitive_key(key: str) -> bool:normalized = key.strip().lower().replace("-", "_")return normalized in _SENSITIVE_KEYS or normalized.endswith(- ("_api_key", "_access_token", "_refresh_token", "_client_secret", "_password")+ (+ "_api_key",+ "_access_token",+ "_refresh_token",+ "_client_secret",+ "_password",+ "_secret",+ "_token",+ ))diff --git a/openviking/resource/feishu_watch_auth.py b/openviking/resource/feishu_watch_auth.pyindex 2415ad1d6..c4a42f022 100644--- a/openviking/resource/feishu_watch_auth.py+++ b/openviking/resource/feishu_watch_auth.py@@ -10,6 +10,8 @@ from dataclasses import dataclassfrom datetime import datetime, timedelta, timezonefrom typing import Any, Dict, Optional+import httpx+FEISHU_AUTH_PROVIDER = "feishu"FEISHU_ACCESS_TOKEN_ARG = "feishu_access_token"FEISHU_REFRESH_TOKEN_ARG = "feishu_refresh_token"@@ -17,6 +19,8 @@ FEISHU_APP_ID_ARG = "feishu_app_id"FEISHU_APP_SECRET_ARG = "feishu_app_secret"FEISHU_REFRESH_GRANT_TYPE = "refresh_token"FEISHU_REFRESH_SKEW = timedelta(minutes=5)+FEISHU_OAUTH_TOKEN_URL = "https://accounts.feishu.cn/oauth/v3/token"+LARK_OAUTH_TOKEN_URL = "https://accounts.larksuite.com/oauth/v3/token"@dataclass(frozen=True)@@ -135,7 +139,7 @@ def apply_feishu_refreshed_token(class FeishuOAuthClient:- """Small wrapper around lark-oapi user and tenant token operations."""+ """Small wrapper around Feishu/Lark user and tenant token operations."""def __init__(self, credentials: FeishuAppCredentials):self._credentials = credentials@@ -195,6 +199,70 @@ class FeishuOAuthClient:return await asyncio.to_thread(self._refresh_user_access_token_sync, refresh_token.strip())def _refresh_user_access_token_sync(self, refresh_token: str) -> FeishuRefreshedToken:+ if refresh_token.count(".") != 2:+ return self._refresh_legacy_user_access_token_sync(refresh_token)++ try:+ response = httpx.post(+ (+ LARK_OAUTH_TOKEN_URL+ if "larksuite.com" in self._credentials.domain.lower()+ else FEISHU_OAUTH_TOKEN_URL+ ),+ data={+ "grant_type": FEISHU_REFRESH_GRANT_TYPE,+ "client_id": self._credentials.app_id,+ "client_secret": self._credentials.app_secret,+ "refresh_token": refresh_token,+ },+ timeout=self._credentials.request_timeout,+ )+ payload = response.json()+ except Exception as exc:+ raise FeishuTokenRefreshError(+ f"Failed to refresh Feishu user token: {exc}",+ permanent=False,+ ) from exc++ if not isinstance(payload, dict):+ raise FeishuTokenRefreshError(+ "Feishu user token refresh response is not an object.",+ permanent=False,+ )++ code = payload.get("code")+ if response.status_code >= 400 or payload.get("error") or code not in (None, 0):+ msg = str(+ payload.get("error_description")+ or payload.get("msg")+ or payload.get("message")+ or payload.get("error")+ or f"HTTP {response.status_code}"+ )+ raise FeishuTokenRefreshError(+ f"Feishu user token refresh failed: code={code}, msg={msg}",+ permanent=_is_permanent_refresh_error(code, msg),+ )++ access_token = payload.get("access_token")+ new_refresh_token = payload.get("refresh_token")+ expires_in = payload.get("expires_in")+ access_token = access_token.strip() if isinstance(access_token, str) else ""+ new_refresh_token = new_refresh_token.strip() if isinstance(new_refresh_token, str) else ""+ if not access_token or not new_refresh_token or not isinstance(expires_in, int):+ raise FeishuTokenRefreshError(+ "Feishu user token refresh response is missing access_token, "+ "refresh_token, or expires_in.",+ permanent=False,+ )++ return FeishuRefreshedToken(+ access_token=access_token,+ refresh_token=new_refresh_token,+ expires_in=expires_in,+ )++ def _refresh_legacy_user_access_token_sync(self, refresh_token: str) -> FeishuRefreshedToken:try:from lark_oapi.api.authen.v1 import (CreateRefreshAccessTokenRequest,@@ -202,7 +270,7 @@ class FeishuOAuthClient:)except ImportError as exc:raise FeishuTokenRefreshError(- "lark-oapi is required to refresh Feishu user tokens. "+ "lark-oapi is required to refresh legacy Feishu user tokens. ""Install it with: pip install lark-oapi>=1.0.0",permanent=True,) from exc@@ -217,7 +285,6 @@ class FeishuOAuthClient:).build())-try:response = self._get_client().authen.v1.refresh_access_token.create(request)except FeishuTokenRefreshError:@@ -238,7 +305,7 @@ class FeishuOAuthClient:data = getattr(response, "data", None)access_token = (getattr(data, "access_token", None) or "").strip()- new_refresh_token = (getattr(data, "refresh_token", None) or refresh_token).strip()+ new_refresh_token = (getattr(data, "refresh_token", None) or "").strip()expires_in = getattr(data, "expires_in", None)if not access_token or not new_refresh_token or not isinstance(expires_in, int):raise FeishuTokenRefreshError(@@ -259,7 +326,7 @@ class FeishuOAuthClient:import lark_oapi as larkexcept ImportError as exc:raise FeishuTokenRefreshError(- "lark-oapi is required to refresh Feishu user tokens. "+ "lark-oapi is required to refresh legacy Feishu user tokens. ""Install it with: pip install lark-oapi>=1.0.0",permanent=True,) from excdiff --git a/openviking/resource/watch_manager.py b/openviking/resource/watch_manager.pyindex 8f8682ab9..9f25167fe 100644--- a/openviking/resource/watch_manager.py+++ b/openviking/resource/watch_manager.py@@ -14,6 +14,7 @@ from typing import Any, Dict, List, Optionalfrom pydantic import BaseModel, Field+from openviking.observability.http_error_context import sanitize_public_http_errorfrom openviking.resource.processing_mode import DEFAULT_PROCESSING_MODE, ProcessingModefrom openviking.resource.uri_mutation_coordinator import (UriMutationCoordinator,@@ -81,8 +82,14 @@ class WatchTask(BaseModel):auth_state: Optional[Dict[str, Any]] = Field(default=None, description="Private authentication state for scheduled re-processing")+ connector_states: Optional[Dict[str, Any]] = Field(+ default=None, description="Private external Connector stream states"+ )created_at: datetime = Field(default_factory=datetime.now, description="Task creation time")last_execution_time: Optional[datetime] = Field(None, description="Last execution time")+ last_task_id: Optional[str] = Field(None, description="Latest ingestion task identifier")+ last_status: Optional[str] = Field(None, description="Latest execution status")+ last_error: Optional[str] = Field(None, description="Latest sanitized execution error")next_execution_time: Optional[datetime] = Field(None, description="Next execution time")is_active: bool = Field(default=True, description="Whether the task is active")account_id: str = Field(default="default", description="Account ID (tenant)")@@ -112,6 +119,9 @@ class WatchTask(BaseModel):"last_execution_time": self.last_execution_time.isoformat()if self.last_execution_timeelse None,+ "last_task_id": self.last_task_id,+ "last_status": self.last_status,+ "last_error": self.last_error,"next_execution_time": self.next_execution_time.isoformat()if self.next_execution_timeelse None,@@ -127,6 +137,8 @@ class WatchTask(BaseModel):data["to_is_directory"] = self.to_is_directoryif self.auth_state is not None:data["auth_state"] = self.auth_state+ if self.connector_states is not None:+ data["connector_states"] = self.connector_statesreturn data@classmethod@@ -143,6 +155,10 @@ class WatchTask(BaseModel):data["processor_kwargs"] = {}if data.get("auth_state") is not None and not isinstance(data.get("auth_state"), dict):data["auth_state"] = None+ if data.get("connector_states") is not None and not isinstance(+ data.get("connector_states"), dict+ ):+ data["connector_states"] = Nonereturn cls(**data)def calculate_next_execution_time(self) -> datetime:@@ -405,6 +421,8 @@ class WatchManager:processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,processor_kwargs: Optional[Dict[str, Any]] = None,auth_state: Optional[Dict[str, Any]] = None,+ connector_states: Optional[Dict[str, Any]] = None,+ is_active: bool = True,) -> WatchTask:"""Create and persist a watch task while its target URI is stable."""if not path:@@ -434,12 +452,16 @@ class WatchManager:processing_mode=processing_mode,processor_kwargs=processor_kwargs or {},auth_state=auth_state,+ connector_states=connector_states,+ is_active=is_active,account_id=account_id,user_id=user_id,original_role=original_role,)- task.next_execution_time = task.calculate_next_execution_time()+ task.next_execution_time = (+ task.calculate_next_execution_time() if task.is_active else None+ )self._tasks[task.task_id] = taskif to_uri:@@ -472,6 +494,7 @@ class WatchManager:processing_mode: Optional[ProcessingMode] = None,processor_kwargs: Optional[Dict[str, Any]] = None,auth_state: Any = _UNSET,+ connector_states: Any = _UNSET,is_active: Optional[bool] = None,) -> WatchTask:"""Update a watch task while its current and requested target URIs are stable."""@@ -514,6 +537,7 @@ class WatchManager:processing_mode=processing_mode,processor_kwargs=processor_kwargs,auth_state=auth_state,+ connector_states=connector_states,is_active=is_active,)@@ -536,6 +560,7 @@ class WatchManager:processing_mode: Optional[ProcessingMode],processor_kwargs: Optional[Dict[str, Any]],auth_state: Any,+ connector_states: Any,is_active: Optional[bool],) -> WatchTask:task_id = task.task_id@@ -579,6 +604,10 @@ class WatchManager:task.processor_kwargs = processor_kwargsif auth_state is not _UNSET:task.auth_state = auth_state+ if connector_states is not _UNSET:+ if connector_states is not None and not isinstance(connector_states, dict):+ raise ValueError("connector_states must be an object")+ task.connector_states = connector_statesif is_active is not None:task.is_active = is_active@@ -619,6 +648,52 @@ class WatchManager:task.auth_state = auth_stateawait self._save_tasks()+ async def update_connector_states(+ self,+ task_id: str,+ connector_states: Dict[str, Any],+ ) -> None:+ """Update private stream states for an external Connector watch."""+ if not isinstance(connector_states, dict):+ raise ValueError("connector_states must be an object")+ async with self._lock:+ task = self._tasks.get(task_id)+ if not task:+ return+ task.connector_states = connector_states+ await self._save_tasks()++ async def record_execution(+ self,+ task_id: str,+ *,+ status: str,+ execution_task_id: Optional[str] = None,+ error: Optional[str] = None,+ ) -> None:+ """Persist the latest execution result exposed by watch APIs."""+ if not status:+ raise ValueError("status is required")+ sanitized_error = (+ sanitize_public_http_error(code="WATCH_EXECUTION_FAILED", message=error).message+ if error+ else None+ )+ async with self._lock:+ task = self._tasks.get(task_id)+ if not task:+ return+ task.last_task_id = execution_task_id+ task.last_status = status+ task.last_error = sanitized_error+ task.last_execution_time = datetime.now()+ task.next_execution_time = (+ task.calculate_next_execution_time()+ if task.is_active and task.watch_interval > 0+ else None+ )+ await self._save_tasks()+def _plan_target_prefix_rewrite_unlocked(self,old_uri: str,@@ -846,7 +921,7 @@ class WatchManager:if active_only and not task.is_active:continuetasks.append(task)- return tasks+ return sorted(tasks, key=lambda task: task.created_at, reverse=True)async def get_task_by_uri(self,diff --git a/openviking/resource/watch_scheduler.py b/openviking/resource/watch_scheduler.pyindex 75b129494..b07ade999 100644--- a/openviking/resource/watch_scheduler.py+++ b/openviking/resource/watch_scheduler.py@@ -43,6 +43,7 @@ class WatchScheduler:"""DEFAULT_CHECK_INTERVAL = 60.0+ DEFAULT_TASK_TIMEOUT = 3 * 60 * 60def __init__(self,@@ -50,6 +51,7 @@ class WatchScheduler:viking_fs: Optional[Any] = None,check_interval: float = DEFAULT_CHECK_INTERVAL,max_concurrency: int = 4,+ task_timeout: float = DEFAULT_TASK_TIMEOUT,uri_mutation_coordinator: Optional[UriMutationCoordinator] = None,):"""Initialize WatchScheduler.@@ -66,14 +68,18 @@ class WatchScheduler:raise ValueError("check_interval must be > 0")if max_concurrency <= 0:raise ValueError("max_concurrency must be > 0")+ if task_timeout <= 0:+ raise ValueError("task_timeout must be > 0")self._check_interval = check_intervalself._max_concurrency = max_concurrency+ self._task_timeout = task_timeoutself._semaphore = asyncio.Semaphore(max_concurrency)self._watch_manager: Optional[WatchManager] = Noneself._running = Falseself._scheduler_task: Optional[asyncio.Task] = Noneself._executing_tasks: Set[str] = set()+ self._execution_tasks: Set[asyncio.Task[None]] = set()self._lock = asyncio.Lock()@property@@ -123,6 +129,13 @@ class WatchScheduler:passself._scheduler_task = None+ execution_tasks = list(self._execution_tasks)+ for task in execution_tasks:+ task.cancel()+ if execution_tasks:+ await asyncio.gather(*execution_tasks, return_exceptions=True)+ self._execution_tasks.clear()+# Clean up WatchManagerif self._watch_manager:self._watch_manager = None@@ -178,9 +191,11 @@ class WatchScheduler:next_time = await self._watch_manager.get_next_execution_time()if next_time is not None:now = datetime.now()+ # Floor at 1s: a due task that is still executing (or held by+ # an in-flight first round) would otherwise spin this loop.sleep_seconds = min(self._check_interval,- max(0.0, (next_time - now).total_seconds()),+ max(1.0, (next_time - now).total_seconds()),)await asyncio.sleep(sleep_seconds)except asyncio.CancelledError:@@ -217,8 +232,21 @@ class WatchScheduler:finally:await asyncio.shield(self._discard_executing(t.task_id))- if tasks_to_run:- await asyncio.gather(*(asyncio.create_task(run_one(t)) for t in tasks_to_run))+ for due_task in tasks_to_run:+ execution = asyncio.create_task(run_one(due_task))+ self._execution_tasks.add(execution)+ execution.add_done_callback(self._on_execution_done)++ def _on_execution_done(self, task: asyncio.Task[None]) -> None:+ self._execution_tasks.discard(task)+ if task.cancelled():+ return+ error = task.exception()+ if error is not None:+ logger.error(+ "[WatchScheduler] Unhandled watch execution error",+ exc_info=(type(error), error, error.__traceback__),+ )async def _execute_task(self, task) -> None:"""Execute a task only after confirming its target URI is stable."""@@ -267,6 +295,9 @@ class WatchScheduler:cancelled = Falseshould_deactivate = Falsedeactivation_reason = ""+ execution_task_id = None+ execution_status = None+ execution_error = Nonetry:auth_state = getattr(task, "auth_state", None)@@ -344,6 +375,9 @@ class WatchScheduler:processor_kwargs["args"] = connector_argsif not should_deactivate:+ refresh_kwargs = {}+ if connector_watch:+ refresh_kwargs["connector_states"] = getattr(task, "connector_states", None)result = await self._resource_service.refresh_resource(path=task.path,ctx=ctx,@@ -357,15 +391,71 @@ class WatchScheduler:processing_mode=getattr(task, "processing_mode", "semantic_and_vectors"),watch_interval=task.watch_interval,enforce_public_remote_targets=True,+ **refresh_kwargs,**processor_kwargs,)- if result.get("status") == "failed":+ execution_task_id = result.get("task_id")+ result_status = str(result.get("status") or "").lower()+ if execution_task_id and result_status not in {+ "completed",+ "failed",+ "cancelled",+ "error",+ }:+ from openviking.service.task_tracker import get_task_tracker++ task_tracker = get_task_tracker()+ try:+ ingestion_task = await task_tracker.wait(+ execution_task_id,+ account_id=task.account_id,+ user_id=task.user_id,+ timeout=self._task_timeout,+ )+ except asyncio.TimeoutError:+ execution_error = (+ f"ingestion task timed out after {self._task_timeout:g}s"+ )+ result_status = "failed"+ try:+ await task_tracker.cancel(+ execution_task_id,+ account_id=task.account_id,+ user_id=task.user_id,+ )+ except Exception:+ logger.exception(+ "[WatchScheduler] Failed to cancel timed-out ingestion "+ "task %s",+ execution_task_id,+ )+ else:+ result_status = ingestion_task.status.value+ execution_error = ingestion_task.error++ if result_status in {"failed", "error"}:+ execution_status = "failed"+ if execution_error is None:+ execution_error = result.get("error")+ if execution_error is None and result.get("errors"):+ execution_error = "; ".join(+ str(error) for error in result["errors"]+ )+ execution_error = execution_error or "watch ingestion failed"logger.warning(f"[WatchScheduler] Task {task.task_id} execution finished with ""a failed ingestion task")+ elif result_status == "cancelled":+ execution_status = "cancelled"else:+ execution_status = "completed"+ if connector_watch and "connector_states" in result:+ await self._watch_manager.update_connector_states(+ task.task_id,+ result["connector_states"],+ )logger.info(f"[WatchScheduler] Task {task.task_id} executed successfully, "f"result: {result.get('root_uri', 'N/A')}"@@ -377,10 +467,14 @@ class WatchScheduler:except FileNotFoundError as e:should_deactivate = Truedeactivation_reason = f"Resource not found: {e}"+ execution_status = "failed"+ execution_error = deactivation_reasonlogger.error(f"[WatchScheduler] Task {task.task_id} resource not found: {e}. Deactivating task.")except Exception as e:+ execution_status = "failed"+ execution_error = str(e) or type(e).__name__logger.error(f"[WatchScheduler] Task {task.task_id} execution failed, "f"error_type={type(e).__name__}"@@ -389,6 +483,17 @@ class WatchScheduler:finally:try:if not cancelled:+ if should_deactivate:+ execution_status = "failed"+ execution_error = deactivation_reason+ await asyncio.shield(+ self._watch_manager.record_execution(+ task.task_id,+ status=execution_status or "completed",+ execution_task_id=execution_task_id,+ error=execution_error,+ )+ )if should_deactivate:await asyncio.shield(self._watch_manager.update_task(@@ -403,9 +508,6 @@ class WatchScheduler:f"[WatchScheduler] Deactivated task {task.task_id}: {deactivation_reason}")else:- await asyncio.shield(- self._watch_manager.update_execution_time(task.task_id)- )logger.info(f"[WatchScheduler] Updated execution time for task {task.task_id}")@@ -415,6 +517,20 @@ class WatchScheduler:exc_info=True,)+ async def hold_execution(self, task_id: str) -> bool:+ """Mark *task_id* as executing outside the scheduler.++ Used while an import's first round runs so a due tick does not start an+ overlapping run; release with :meth:`release_execution`.+ """+ held = await self._try_mark_executing(task_id)+ logger.debug(f"[WatchScheduler] hold_execution task_id={task_id} held={held}")+ return held++ async def release_execution(self, task_id: str) -> None:+ await self._discard_executing(task_id)+ logger.debug(f"[WatchScheduler] release_execution task_id={task_id}")+async def _try_mark_executing(self, task_id: str) -> bool:async with self._lock:if task_id in self._executing_tasks:diff --git a/openviking/server/routers/resources.py b/openviking/server/routers/resources.pyindex 429e6e44f..4a090c42e 100644--- a/openviking/server/routers/resources.py+++ b/openviking/server/routers/resources.py@@ -24,9 +24,6 @@ from openviking_cli.exceptions import InvalidArgumentErrorrouter = APIRouter(prefix="/api/v1", tags=["resources"])-_CONNECTOR_TASK_ORIGIN_HEADER = "X-OpenViking-Task-Origin"-_CONNECTOR_TASK_ORIGIN = "connector_import"-class AddResourceRequest(BaseModel):"""Request model for add_resource.@@ -56,6 +53,7 @@ class AddResourceRequest(BaseModel):Default is False (async processing).timeout: Timeout in seconds when wait=True. None means no timeout.strict: Whether to use strict mode for processing. Default is True.+ internal_task: Whether to hide this task from the default task list.ignore_dirs: Comma-separated list of directory names to ignore during parsing.include: Glob pattern for files to include during parsing.exclude: Glob pattern for files to exclude during parsing.@@ -78,10 +76,13 @@ class AddResourceRequest(BaseModel):Note: Re-adding the same source to the same target updates its active watch task.A different source targeting an active watch raises ConflictError; cancel that- watch first with watch_interval <= 0. For Connector imports this check is- eventually consistent: the Watch is created only after the background import- succeeds, so overlapping imports may both write before Watch finalization- reports the conflict.+ watch first with watch_interval <= 0. Connector imports create the Watch before+ the import runs, so it is visible immediately and the conflict is reported at+ submission; the scheduler does not run it until the first round has recorded+ its result.+ is_active: Initial Watch state for Connector and native Feishu imports. When false,+ requires watch_interval > 0 and an explicit to or parent target and creates the Watch+ paused; it stays paused until updated, regardless of the import result."""model_config = ConfigDict(extra="forbid")@@ -97,6 +98,7 @@ class AddResourceRequest(BaseModel):wait: bool = Falsetimeout: Optional[float] = Nonestrict: bool = False+ internal_task: bool = Falsesource_name: Optional[str] = Noneignore_dirs: Optional[str] = Noneinclude: Optional[str] = None@@ -106,6 +108,7 @@ class AddResourceRequest(BaseModel):args: Dict[str, Any] = Field(default_factory=dict)telemetry: TelemetryRequest = Falsewatch_interval: float = 0+ is_active: bool = Trueprocessing_mode: ProcessingMode = DEFAULT_PROCESSING_MODEtags: Optional[list[str]] = Nonetag_mode: str = "replace"@@ -130,6 +133,15 @@ class AddResourceRequest(BaseModel):raise ValueError("'add_type' requires an exact 'to' target")return self+ @model_validator(mode="after")+ def check_paused_watch(self):+ has_target = bool((self.to or "").strip() or (self.parent or "").strip())+ if self.is_active is False and (self.watch_interval <= 0 or not has_target):+ raise ValueError(+ "is_active=false requires watch_interval > 0 and either 'to' or 'parent'"+ )+ return self+class AddSkillRequest(BaseModel):"""Request model for add_skill.@@ -301,10 +313,8 @@ async def add_resource(tag_mode=request.tag_mode,allow_local_path_resolution=allow_local_path_resolution,enforce_public_remote_targets=True,- internal_task=(- http_request.headers.get(_CONNECTOR_TASK_ORIGIN_HEADER, "").strip().lower()- == _CONNECTOR_TASK_ORIGIN- ),+ internal_task=request.internal_task,+ is_active=request.is_active,args=request.args,**kwargs,)diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.pyindex f53f2e7a8..0cf0fe32c 100644--- a/openviking/service/resource_service.py+++ b/openviking/service/resource_service.py@@ -21,6 +21,7 @@ from urllib.parse import urlparsefrom uuid import uuid4from openviking.core.namespace import is_content_root_uri+from openviking.observability.http_error_context import sanitize_public_http_errorfrom openviking.parse.backend import ParserBackend, normalize_parser_backendfrom openviking.parse.mode import ParseMode, normalize_parse_modefrom openviking.parse.parsers.constants import MPEG_TS_EXTENSION_ALIAS@@ -89,7 +90,7 @@ if TYPE_CHECKING:from openviking.connector.delegate import ConnectorDelegatefrom openviking.parse.accessors.base import LocalResourcefrom openviking.resource.staged_source import StagedSource- from openviking.resource.watch_manager import WatchManager+ from openviking.resource.watch_manager import WatchManager, WatchTaskfrom openviking.resource.watch_scheduler import WatchSchedulerfrom openviking.service.resource_memory_link_service import ResourceMemoryLinkService@@ -111,6 +112,7 @@ _ADD_RESOURCE_ARGS_RESERVED_FIELDS = frozenset("summarize","processing_mode","watch_interval",+ "is_active","skip_watch_management","allow_local_path_resolution","enforce_public_remote_targets",@@ -229,6 +231,52 @@ class ResourceService:return Nonereturn self._watch_scheduler.watch_manager+ async def _hold_watch_execution(self, task_id: str) -> bool:+ """Keep the scheduler from running *task_id* while its first round is in flight."""+ hold = getattr(self._watch_scheduler, "hold_execution", None)+ if hold is None:+ return True+ return bool(await hold(task_id))++ async def _release_watch_execution(self, task_id: str) -> None:+ release = getattr(self._watch_scheduler, "release_execution", None)+ if release is not None:+ await release(task_id)++ async def record_watch_execution(+ self,+ task_id: str,+ *,+ status: str,+ execution_task_id: Optional[str] = None,+ error: Optional[str] = None,+ ) -> None:+ """Record a pre-created Watch's first round and release its scheduler hold."""+ sanitized_error = (+ sanitize_public_http_error(code="WATCH_EXECUTION_FAILED", message=error).message+ if error+ else None+ )+ try:+ watch_manager = self._get_watch_manager()+ if watch_manager is not None:+ await watch_manager.record_execution(+ task_id,+ status=status,+ execution_task_id=execution_task_id,+ error=error,+ )+ logger.info(+ "[ResourceService] Watch first round recorded: watch_task_id=%s status=%s "+ "execution_task_id=%s%s",+ task_id,+ status,+ execution_task_id,+ f" error={sanitized_error}" if sanitized_error else "",+ )+ finally:+ await self._release_watch_execution(task_id)+def _sanitize_watch_processor_kwargs(self, processor_kwargs: Dict[str, Any]) -> Dict[str, Any]:sanitized: Dict[str, Any] = {}for key, value in processor_kwargs.items():@@ -324,6 +372,9 @@ class ResourceService:watch_auth_state: Optional[Dict[str, Any]],ctx: RequestContext,source_type: Optional[str] = None,+ connector_states: Optional[Dict[str, Any]] = None,+ is_active: Optional[bool] = None,+ on_watch_ready: Optional[Callable[[str], None]] = None,) -> None:if not watch_manager or not manage_watch:return@@ -352,7 +403,7 @@ class ResourceService:)try:sanitized = self._sanitize_watch_processor_kwargs(processor_kwargs)- await self._handle_watch_task_creation(+ watch = await self._handle_watch_task_creation(path=path,to_uri=watch_to,to_is_directory=to_is_directory,@@ -365,9 +416,13 @@ class ResourceService:processing_mode=processing_mode,processor_kwargs=sanitized,auth_state=watch_auth_state,+ connector_states=connector_states,source_type=source_type or self._infer_watch_source_type(path),ctx=ctx,+ is_active=is_active is not False,)+ if watch is not None and on_watch_ready is not None:+ on_watch_ready(watch.task_id)except ConflictError:raiseexcept Exception as e:@@ -601,10 +656,9 @@ class ResourceService:account_id=msg.account_id,user_id=msg.user_id,task_id=msg.task_id,- meta={- "source_path": msg.source_path,- **({"internal": True} if msg.internal_task else {}),- },+ meta=(+ {"internal": True} if msg.internal_task else {"source_path": msg.source_path}+ ),auth=task_auth,)await get_queue_manager().enqueue(queue_name, msg.to_dict())@@ -724,6 +778,7 @@ class ResourceService:processing_mode=msg.processing_mode,parse_mode=msg.parse_mode,watch_interval=msg.watch_interval,+ is_active=msg.is_active,manage_watch=not msg.skip_watch_management,tags=msg.tags,tag_mode=msg.tag_mode,@@ -734,6 +789,7 @@ class ResourceService:watch_auth_state=watch_auth_state,prepared_resource=prepared_resource,internal_task=msg.internal_task,+ on_watch_ready=lambda task_id: setattr(msg, "watch_task_id", task_id),**internal_kwargs,)except BaseException:@@ -1034,6 +1090,7 @@ class ResourceService:enforce_public_remote_targets: bool,processor_kwargs: Dict[str, Any],internal_task: bool,+ is_active: Optional[bool] = None,) -> Dict[str, Any]:from openviking.storage.queuefs.add_resource_msg import AddResourceMsg@@ -1097,6 +1154,7 @@ class ResourceService:processing_mode=processing_mode,parse_mode=mode.value,watch_interval=watch_interval,+ is_active=is_active,skip_watch_management=not manage_watch,tags=tags,tag_mode=tag_mode,@@ -1159,7 +1217,7 @@ class ResourceService:await self._viking_fs.delete_temp(plan.staged_source.temp_uri, ctx=ctx)raise- response = {"status": "success", "task_id": task.task_id}+ response = {"status": "success", "task_id": task.task_id, "source_path": msg.source_path}if not defer_target_resolution:response["root_uri"] = root_urireturn response@@ -1297,6 +1355,7 @@ class ResourceService:summarize: bool = False,processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,watch_interval: float = 0,+ is_active: Optional[bool] = None,tags: Optional[List[str]] = None,tag_mode: str = "replace",allow_local_path_resolution: bool = True,@@ -1329,6 +1388,7 @@ class ResourceService:summarize=summarize,processing_mode=processing_mode,watch_interval=watch_interval,+ is_active=is_active,manage_watch=True,tags=tags,tag_mode=tag_mode,@@ -1357,6 +1417,7 @@ class ResourceService:enforce_public_remote_targets: bool = False,add_type: Optional[str] = None,args: Optional[Dict[str, Any]] = None,+ connector_states: Optional[Dict[str, Any]] = None,**kwargs,) -> Dict[str, Any]:"""Submit a scheduled refresh without changing its watch task."""@@ -1379,6 +1440,7 @@ class ResourceService:enforce_public_remote_targets=enforce_public_remote_targets,add_type=add_type,args=args,+ connector_states=connector_states,**kwargs,)@@ -1398,6 +1460,7 @@ class ResourceService:summarize: bool = False,processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,watch_interval: float = 0,+ is_active: Optional[bool] = None,manage_watch: bool = True,tags: Optional[List[str]] = None,tag_mode: str = "replace",@@ -1406,6 +1469,7 @@ class ResourceService:enforce_public_remote_targets: bool = False,internal_task: bool = False,args: Optional[Dict[str, Any]] = None,+ connector_states: Optional[Dict[str, Any]] = None,**kwargs,) -> Dict[str, Any]:"""Validate and route one resource ingestion request.@@ -1445,8 +1509,11 @@ class ResourceService:Note: Re-adding the same source to the same target updates its active watchtask in place. A different source targeting an active watch raisesConflictError; cancel that watch first with watch_interval <= 0. Connector- imports create the Watch only after the background import succeeds, so this- conflict may be reported after both overlapping imports have written data.+ imports create the Watch before the import runs, so the conflict is reported+ at submission and the Watch is visible at once; the scheduler holds it until+ the first round records its result.+ is_active: When false, the Connector or native Feishu Watch is created paused.+ Requires watch_interval > 0 and an explicit to or parent target.enforce_public_remote_targets: When True, reject non-public remote hosts andvalidate each outbound HTTP request URL during fetch.args: Parser/accessor-specific options forwarded to the processing chain.@@ -1460,6 +1527,11 @@ class ResourceService:InvalidArgumentError: If the URI scope is not 'resources'"""self._ensure_initialized()+ has_target = bool((to or "").strip() or (parent or "").strip())+ if is_active is False and (watch_interval <= 0 or not has_target):+ raise InvalidArgumentError(+ "is_active=false requires watch_interval > 0 and either 'to' or 'parent'."+ )processing_mode = normalize_processing_mode(processing_mode)self._validate_add_resource_tag_policy(tags=tags, tag_mode=tag_mode)from openviking.connector.delegate import ConnectorDelegate@@ -1533,7 +1605,7 @@ class ResourceService:target_create_parent = bool(kwargs.get("create_parent", False))connector = self._connector- if connector.should_delegate(+ delegate_to_connector = connector.should_delegate(path,ctx=ctx,declared_add_type=add_type,@@ -1548,24 +1620,15 @@ class ResourceService:watch_interval=watch_interval,connector_args=normalized_args.processor_kwargs,kwargs=kwargs,- ):+ )+ if delegate_to_connector:resolved = connector.resolve_add_type(path, add_type)if resolved is None: # pragma: no cover - should_delegate already resolved itraise InvalidArgumentError(f"'{path}' does not match any Connector source type.")watch_manager = self._get_watch_manager()watch_auth_state = None- defer_watch_creation = bool(watch_manager and manage_watch and watch_interval > 0)- if defer_watch_creation and watch_manager:- # Connector imports may run for a long time. This is only a best-effort- # precheck: the authoritative conflict check happens when on_success- # creates the Watch, after the Connector may already have written data.- await watch_manager.get_upsertable_task_by_uri(- path=path,- to_uri=target_to,- account_id=ctx.account_id,- user_id=ctx.user.user_id,- role=str(ctx.role),- )+ create_watch = bool(watch_manager and manage_watch and watch_interval > 0)+ if create_watch:watch_auth_state = await connector.create_watch_auth_state(api_key=ctx.api_key or "",account_id=ctx.account_id,@@ -1582,45 +1645,102 @@ class ResourceService:tags,tag_mode,)- on_success: Optional[Callable[[], Awaitable[None]]] = None- if defer_watch_creation:+ on_success: Optional[Callable[[Optional[Dict[str, Any]]], Awaitable[None]]] = None+ on_complete: Optional[+ Callable[[str, Optional[str], Optional[str]], Awaitable[None]]+ ] = None+ if create_watch:+ # The Watch exists before the import runs so it is visible at once.+ # A scheduler hold keeps the first round from overlapping with a+ # scheduled run; record_watch_execution releases it at the end.+ watch = await self._handle_watch_task_creation(+ path=path,+ to_uri=target_to,+ to_is_directory=to_is_directory,+ parent_uri=target_parent,+ reason=reason,+ instruction=instruction,+ watch_interval=watch_interval,+ build_index=build_index,+ summarize=summarize,+ processing_mode=processing_mode,+ processor_kwargs=connector_watch_processor_kwargs,+ auth_state=watch_auth_state,+ connector_states=None,+ source_type=resolved[0],+ ctx=ctx,+ is_active=is_active is not False,+ hold_execution=True,+ )+ if watch is None:+ raise InternalError("Failed to create Connector watch task.")+ watch_task_id = watch.task_id+ logger.info(+ "[ResourceService] Connector watch ready before import: watch_task_id=%s "+ "to=%s add_type=%s is_active=%s",+ watch_task_id,+ target_to,+ resolved[0],+ watch.is_active,+ )- async def create_watch_after_success() -> None:- await self._manage_watch_if_needed(- watch_manager=watch_manager,- manage_watch=True,- watch_interval=watch_interval,- to=target_to,- parent=target_parent,- to_is_directory=to_is_directory,- root_uri=target_to,- path=path,- reason=reason,- instruction=instruction,- build_index=build_index,- summarize=summarize,- processing_mode=processing_mode,- processor_kwargs=connector_watch_processor_kwargs,- watch_auth_state=watch_auth_state,- ctx=ctx,- source_type=resolved[0],- )+ async def record_first_run(+ status: str,+ execution_task_id: Optional[str],+ error: Optional[str],+ ) -> None:+ try:+ await self.record_watch_execution(+ watch_task_id,+ status=status,+ execution_task_id=execution_task_id,+ error=error,+ )+ except Exception:+ logger.exception(+ "[ResourceService] Failed to record Connector watch result"+ )- on_success = create_watch_after_success- result = await connector.submit(- path=path,- ctx=ctx,- declared_add_type=add_type,- to=target_to,- reason=reason,- connector_args=normalized_args.processor_kwargs,- tags=tags,- tag_mode=tag_mode,- wait_for_completion=not manage_watch and watch_interval > 0,- on_success=on_success,- **kwargs,- )- if not defer_watch_creation:+ async def store_connector_states(+ new_connector_states: Optional[Dict[str, Any]] = None,+ ) -> None:+ if new_connector_states is not None:+ await watch_manager.update_connector_states(+ watch_task_id,+ new_connector_states,+ )++ on_complete = record_first_run+ on_success = store_connector_states+ try:+ result = await connector.submit(+ path=path,+ ctx=ctx,+ declared_add_type=add_type,+ to=target_to,+ reason=reason,+ connector_args=normalized_args.processor_kwargs,+ tags=tags,+ tag_mode=tag_mode,+ wait_for_completion=not manage_watch and watch_interval > 0,+ connector_states=connector_states,+ on_success=on_success,+ on_complete=on_complete,+ **kwargs,+ )+ except asyncio.CancelledError:+ if on_complete is not None:+ await on_complete(+ "failed",+ None,+ "connector task submission cancelled",+ )+ raise+ except Exception as exc:+ if on_complete is not None:+ await on_complete("failed", None, str(exc))+ raise+ if not create_watch:await self._manage_watch_if_needed(watch_manager=watch_manager,manage_watch=manage_watch,@@ -1642,6 +1762,12 @@ class ResourceService:)return result+ from openviking.parse.accessors.feishu_accessor import FeishuAccessor++ if is_active is False and not FeishuAccessor._is_feishu_url(path):+ raise InvalidArgumentError(+ "is_active=false is only supported for Connector or native Feishu imports."+ )if enforce_public_remote_targets and is_remote_resource_source(path):path = require_remote_resource_source(path)kwargs.setdefault("request_validator", ensure_public_remote_target)@@ -1677,6 +1803,7 @@ class ResourceService:enforce_public_remote_targets=enforce_public_remote_targets,processor_kwargs=kwargs,internal_task=internal_task,+ is_active=is_active,)else:result = await self._execute_resource_ingestion(@@ -1694,6 +1821,7 @@ class ResourceService:processing_mode=processing_mode,parse_mode=mode,watch_interval=watch_interval,+ is_active=is_active,manage_watch=manage_watch,tags=tags,tag_mode=tag_mode,@@ -1753,6 +1881,7 @@ class ResourceService:processing_mode: ProcessingMode = DEFAULT_PROCESSING_MODE,parse_mode: ParseMode | str = ParseMode.DEFAULT,watch_interval: float = 0,+ is_active: Optional[bool] = None,manage_watch: bool = True,tags: Optional[List[str]] = None,tag_mode: str = "replace",@@ -1763,6 +1892,7 @@ class ResourceService:stage_callback: Optional[Callable[[str], Any]] = None,prepared_resource: Optional["LocalResource"] = None,internal_task: bool = False,+ on_watch_ready: Optional[Callable[[str], None]] = None,**kwargs,) -> Dict[str, Any]:"""Execute an already-routed resource ingestion."""@@ -1837,6 +1967,7 @@ class ResourceService:watch_manager=watch_manager,manage_watch=manage_watch,watch_interval=watch_interval,+ is_active=is_active,to=target_to,parent=target_parent,to_is_directory=watch_to_is_directory,@@ -1850,6 +1981,7 @@ class ResourceService:processor_kwargs=self._watch_processor_kwargs(kwargs, tags, tag_mode),watch_auth_state=watch_auth_state,ctx=ctx,+ on_watch_ready=on_watch_ready,)if defer_post_processing:from openviking.storage.queuefs.add_resource_msg import AddResourceMsg@@ -2055,9 +2187,12 @@ class ResourceService:processing_mode: ProcessingMode,processor_kwargs: Dict[str, Any],auth_state: Optional[Dict[str, Any]],+ connector_states: Optional[Dict[str, Any]],source_type: Optional[str],ctx: RequestContext,- ) -> None:+ is_active: bool = True,+ hold_execution: bool = False,+ ) -> Optional["WatchTask"]:"""Handle creation or update of watch task.Args:@@ -2074,7 +2209,7 @@ class ResourceService:"""watch_manager = self._get_watch_manager()if not watch_manager:- return+ return Noneexisting_task = await watch_manager.get_upsertable_task_by_uri(path=path,@@ -2083,52 +2218,79 @@ class ResourceService:user_id=ctx.user.user_id,role=str(ctx.role),)- if existing_task:- was_active = existing_task.is_active- await watch_manager.update_task(- task_id=existing_task.task_id,- account_id=ctx.account_id,- user_id=ctx.user.user_id,- role=str(ctx.role),- path=path,- source_type=source_type,- to_uri=to_uri,- to_is_directory=to_is_directory,- parent_uri=parent_uri,- reason=reason,- instruction=instruction,- watch_interval=watch_interval,- build_index=build_index,- summarize=summarize,- processing_mode=processing_mode,- processor_kwargs=processor_kwargs,- auth_state=auth_state,- is_active=True,- )- logger.info(- f"[ResourceService] {'Updated active' if was_active else 'Reactivated and updated'} "- f"watch task {existing_task.task_id} for {to_uri}"- )- else:- task = await watch_manager.create_task(- path=path,- account_id=ctx.account_id,- user_id=ctx.user.user_id,- original_role=str(ctx.role),- source_type=source_type,- to_uri=to_uri,- to_is_directory=to_is_directory,- parent_uri=parent_uri,- reason=reason,- instruction=instruction,- watch_interval=watch_interval,- build_index=build_index,- summarize=summarize,- processing_mode=processing_mode,- processor_kwargs=processor_kwargs,- auth_state=auth_state,- )- logger.info(f"[ResourceService] Created watch task {task.task_id} for {to_uri}")+ held_task_id: Optional[str] = None+ try:+ if existing_task:+ if hold_execution:+ if not await self._hold_watch_execution(existing_task.task_id):+ raise ConflictError(+ f"Watch task {existing_task.task_id} is already executing. "+ "Retry after it finishes.",+ resource=to_uri,+ )+ held_task_id = existing_task.task_id+ was_active = existing_task.is_active+ task = await watch_manager.update_task(+ task_id=existing_task.task_id,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ role=str(ctx.role),+ path=path,+ source_type=source_type,+ to_uri=to_uri,+ to_is_directory=to_is_directory,+ parent_uri=parent_uri,+ reason=reason,+ instruction=instruction,+ watch_interval=watch_interval,+ build_index=build_index,+ summarize=summarize,+ processing_mode=processing_mode,+ processor_kwargs=processor_kwargs,+ auth_state=auth_state,+ connector_states=connector_states,+ is_active=is_active,+ )+ logger.info(+ f"[ResourceService] "+ f"{'Updated active' if was_active else 'Reactivated and updated'} "+ f"watch task {existing_task.task_id} for {to_uri}"+ )+ else:+ task = await watch_manager.create_task(+ path=path,+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ original_role=str(ctx.role),+ source_type=source_type,+ to_uri=to_uri,+ to_is_directory=to_is_directory,+ parent_uri=parent_uri,+ reason=reason,+ instruction=instruction,+ watch_interval=watch_interval,+ build_index=build_index,+ summarize=summarize,+ processing_mode=processing_mode,+ processor_kwargs=processor_kwargs,+ auth_state=auth_state,+ connector_states=connector_states,+ is_active=is_active,+ )+ if hold_execution:+ if not await self._hold_watch_execution(task.task_id):+ raise ConflictError(+ f"Watch task {task.task_id} is already executing. "+ "Retry after it finishes.",+ resource=to_uri,+ )+ held_task_id = task.task_id+ logger.info(f"[ResourceService] Created watch task {task.task_id} for {to_uri}")+ return task+ except BaseException:+ if held_task_id is not None:+ await self._release_watch_execution(held_task_id)+ raiseasync def _handle_watch_task_cancellation(self, to_uri: str, ctx: RequestContext) -> None:"""Handle cancellation of watch task.diff --git a/openviking/service/task_tracker.py b/openviking/service/task_tracker.pyindex 37ad6c4ec..5c8ae5140 100644--- a/openviking/service/task_tracker.py+++ b/openviking/service/task_tracker.py@@ -552,6 +552,20 @@ class TaskTracker:error=error,)+ async def mark_cancelled(+ self,+ task_id: str,+ account_id: Optional[str] = None,+ user_id: Optional[str] = None,+ ) -> None:+ """Record an externally observed cancellation without requesting local cancellation."""+ await self._record_outcome(+ task_id,+ account_id,+ user_id,+ terminal_status=TaskStatus.CANCELLED,+ )+async def _record_outcome(self,task_id: str,@@ -561,6 +575,7 @@ class TaskTracker:result: Optional[Dict[str, Any]] = None,error: Optional[str] = None,resource_id: Optional[str] = None,+ terminal_status: Optional[TaskStatus] = None,) -> None:await self._dispatcher.run(lambda: self._record_outcome_on_owner(@@ -570,6 +585,7 @@ class TaskTracker:result=result,error=error,resource_id=resource_id,+ terminal_status=terminal_status,))@@ -582,6 +598,7 @@ class TaskTracker:result: Optional[Dict[str, Any]],error: Optional[str],resource_id: Optional[str],+ terminal_status: Optional[TaskStatus],) -> None:cancellation: asyncio.CancelledError | None = Noneoutcome_persisted = False@@ -598,6 +615,9 @@ class TaskTracker:work_error = self._work_index.failure(task_id)if work_error and updated.error is None:updated.error = _sanitize_error(work_error)+ if terminal_status is not None:+ updated.status = terminal_status+ updated.stage = terminal_status.valueupdated.updated_at = self._next_updated_at(task)updated.auth = {}try:diff --git a/openviking/storage/queuefs/add_resource_msg.py b/openviking/storage/queuefs/add_resource_msg.pyindex d493e7929..27fa3057e 100644--- a/openviking/storage/queuefs/add_resource_msg.py+++ b/openviking/storage/queuefs/add_resource_msg.py@@ -50,6 +50,8 @@ class AddResourceMsg:source_name: Optional[str] = Noneto_is_directory: Optional[bool] = Nonewatch_interval: float = 0+ is_active: Optional[bool] = None+ watch_task_id: Optional[str] = Noneskip_watch_management: bool = Truedefer_target_resolution: bool = Falsecleanup_empty_target_on_failure: bool = False@@ -184,6 +186,12 @@ class AddResourceMsg:staged_source=staged_source,job_phase=job_phase,watch_interval=float(data.get("watch_interval", 0) or 0),+ is_active=(data.get("is_active") if isinstance(data.get("is_active"), bool) else None),+ watch_task_id=(+ str(data["watch_task_id"])+ if isinstance(data.get("watch_task_id"), str) and data["watch_task_id"]+ else None+ ),skip_watch_management=bool(data.get("skip_watch_management", True)),defer_target_resolution=bool(data.get("defer_target_resolution", False)),cleanup_empty_target_on_failure=bool(diff --git a/openviking/storage/queuefs/add_resource_processor.py b/openviking/storage/queuefs/add_resource_processor.pyindex b95294593..263c3f8ce 100644--- a/openviking/storage/queuefs/add_resource_processor.py+++ b/openviking/storage/queuefs/add_resource_processor.py@@ -89,6 +89,28 @@ class AddResourceProcessor(DequeueHandlerBase):with suppress(Exception):await self._cleanup_staged_source(msg, ctx)+ async def _record_watch_execution(+ self,+ msg: AddResourceMsg,+ status: str,+ error: Optional[str] = None,+ ) -> None:+ if not msg.watch_task_id:+ return+ try:+ await self._resource_service.record_watch_execution(+ msg.watch_task_id,+ status=status,+ execution_task_id=msg.task_id,+ error=error,+ )+ except Exception:+ logger.exception("[AddResource] Failed to record initial Watch execution")++ async def _handle_cancelled(self, msg: AddResourceMsg, ctx: RequestContext) -> None:+ await self._release_cancelled_resources(msg, ctx)+ await self._record_watch_execution(msg, "cancelled")+async def _requeue_lock_handoff(self, msg: AddResourceMsg, exc: Exception) -> bool:if msg.lock_handoff_retry >= 2:return False@@ -123,10 +145,7 @@ class AddResourceProcessor(DequeueHandlerBase):account_id=ctx.account_id,user_id=ctx.user.user_id,task_id=msg.task_id,- meta={- "source_path": msg.source_path,- **({"internal": True} if msg.internal_task else {}),- },+ meta=({"internal": True} if msg.internal_task else {"source_path": msg.source_path}),)if task.status in (TaskStatus.CANCELLING,@@ -139,6 +158,12 @@ class AddResourceProcessor(DequeueHandlerBase):else:with suppress(Exception):await self._cleanup_staged_source(msg, ctx)+ status = (+ "cancelled"+ if task.status in (TaskStatus.CANCELLING, TaskStatus.CANCELLED)+ else task.status.value+ )+ await self._record_watch_execution(msg, status, getattr(task, "error", None))unregister_telemetry(telemetry_id)self.report_success()return None@@ -158,6 +183,11 @@ class AddResourceProcessor(DequeueHandlerBase):account_id=ctx.account_id,user_id=ctx.user.user_id,)+ await self._record_watch_execution(+ msg,+ "failed",+ f"Invalid lock_handoff: {exc}",+ )self.report_error(f"Invalid lock_handoff: {exc}", data)unregister_telemetry(telemetry_id)with suppress(Exception):@@ -210,25 +240,28 @@ class AddResourceProcessor(DequeueHandlerBase):)if result.get("status") == "error":errors = result.get("errors") or ["resource processing failed"]+ error = "; ".join(str(error) for error in errors)code = result.get("code")failure_result = {"code": code} if isinstance(code, str) and code else Noneawait tracker.fail(msg.task_id,- "; ".join(str(error) for error in errors),+ error,account_id=ctx.account_id,user_id=ctx.user.user_id,result=failure_result,)+ await self._record_watch_execution(msg, "failed", error)terminal = Trueself.report_error("resource processing failed", data)return None- await tracker.complete(- msg.task_id,- deepcopy(result),- account_id=ctx.account_id,- user_id=ctx.user.user_id,- resource_id=result.get("root_uri"),- )+ if not msg.watch_task_id:+ await tracker.complete(+ msg.task_id,+ deepcopy(result),+ account_id=ctx.account_id,+ user_id=ctx.user.user_id,+ resource_id=result.get("root_uri"),+ )else:result = deepcopy(replay_result)await tracker.wait_for_descendants(msg.task_id, metadata.work_id)@@ -260,6 +293,7 @@ class AddResourceProcessor(DequeueHandlerBase):source_name=msg.source_name,timeout=msg.timeout,)+ await self._record_watch_execution(msg, "completed")await tracker.complete(msg.task_id,result,@@ -270,7 +304,16 @@ class AddResourceProcessor(DequeueHandlerBase):terminal = Trueself.report_success()return None+ except asyncio.CancelledError:+ await self._record_watch_execution(msg, "cancelled")+ terminal = True+ raiseexcept Exception as exc:+ await self._record_watch_execution(+ msg,+ "failed",+ str(exc) or type(exc).__name__,+ )failure_result = ({"code": exc.code} if isinstance(exc, OpenVikingError) and exc.code else None)@@ -305,7 +348,7 @@ class AddResourceProcessor(DequeueHandlerBase):self.report_error(str(exc), data)return Nonefuture = asyncio.run_coroutine_threadsafe(- self._release_cancelled_resources(+ self._handle_cancelled(msg,RequestContext(user=UserIdentifier(msg.account_id, msg.user_id),diff --git a/tests/cli/test_cli_filesystem.py b/tests/cli/test_cli_filesystem.pyindex b8eaea039..1a9cee45b 100644--- a/tests/cli/test_cli_filesystem.py+++ b/tests/cli/test_cli_filesystem.py@@ -138,26 +138,19 @@ class TestFsMv:class TestFsCp:- def test_cp_file_json_and_human_output(self, test_dir_uri):+ def test_cp_file_json_and_human_output(self, test_dir_uri, test_file_uri):suffix = uuid.uuid4().hex[:6]- source = f"{test_dir_uri}/cp_source_{suffix}.txt"json_target = f"{test_dir_uri}/cp_json_{suffix}.txt"human_target = f"{test_dir_uri}/cp_human_{suffix}.txt"- with tempfile.NamedTemporaryFile(suffix=".txt", delete=False, mode="w") as f:- f.write("copy me")- temp_path = f.nametry:- assert ov_add_resource(temp_path, source)["exit_code"] == 0- json_result = ov_cp(source, json_target)+ json_result = ov_cp(test_file_uri, json_target)assert json_result["exit_code"] == 0, json_result["stderr"]assert json_result["json"] and json_result["json"].get("ok") is True- human_result = ov_cp(source, human_target, output=None)+ human_result = ov_cp(test_file_uri, human_target, output=None)assert human_result["exit_code"] == 0, human_result["stderr"]assert "Copied:" in human_result["stdout"]finally:- os.unlink(temp_path)- ov_rm(source)ov_rm(json_target)ov_rm(human_target)diff --git a/tests/connector/test_client.py b/tests/connector/test_client.pyindex 501cbb688..76da6a0c4 100644--- a/tests/connector/test_client.py+++ b/tests/connector/test_client.py@@ -74,6 +74,23 @@ async def test_submit_doc_add_sends_controlled_payload_and_auth_headers():]+async def test_submit_doc_add_forwards_external_connector_stream_states():+ _FakeAsyncClient.response_payload = {"code": 0, "data": {"task_key": "connector-1"}}+ client = ConnectorClient("https://connector/doc/add", "https://tracker/task/info", "acct")+ stream_states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00+00:00"}}}++ await client.submit_doc_add(+ add_type="tos",+ api_key="secret",+ tos_path="bucket/prefix",+ to="viking://resources/imports",+ stream_states=stream_states,+ )++ assert _FakeAsyncClient.calls[0]["json"]["stream_states"] == stream_states++@pytest.mark.asyncioasync def test_submit_doc_add_carries_non_tos_source_in_param_config():_FakeAsyncClient.response_payload = {"code": 0, "data": {"task_key": "connector-1"}}@@ -144,15 +161,23 @@ async def test_submit_doc_add_keeps_credentials_out_of_param_config():@pytest.mark.asyncioasync def test_get_task_info_unwraps_task_object():+ stream_states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00Z"}}}_FakeAsyncClient.response_payload = {"code": 0,- "data": {"Task": {"TaskKey": "connector-1", "Status": "running"}},+ "data": {+ "Task": {"TaskKey": "connector-1", "Status": "running"},+ "StreamStates": stream_states,+ },}client = ConnectorClient("https://connector/doc/add", "https://tracker/task/info")result = await client.get_task_info("connector-1", "Bearer secret")- assert result == {"TaskKey": "connector-1", "Status": "running"}+ assert result == {+ "TaskKey": "connector-1",+ "Status": "running",+ "StreamStates": stream_states,+ }assert _FakeAsyncClient.calls[0]["json"] == {"TaskKey": "connector-1"}assert _FakeAsyncClient.calls[0]["headers"] == {"Authorization": "Bearer secret"}diff --git a/tests/integration/test_watch_e2e.py b/tests/integration/test_watch_e2e.pyindex 11e430ae6..1e4b84579 100644--- a/tests/integration/test_watch_e2e.py+++ b/tests/integration/test_watch_e2e.py@@ -189,7 +189,9 @@ class TestWatchE2EConflictDetection:"""End-to-end tests for conflict detection."""@pytest.mark.asyncio- async def test_conflict_when_active_watch_exists(self, e2e_service, watch_test_file: Path):+ async def test_conflict_when_active_watch_exists(+ self, e2e_service, watch_test_file: Path, tmp_path: Path+ ):"""Test that conflict is raised when trying to watch an already watched URI."""service, ctx = e2e_service@@ -202,10 +204,12 @@ class TestWatchE2EConflictDetection:watch_interval=30.0,)+ conflicting_file = tmp_path / "watch_conflict.md"+ conflicting_file.write_text(watch_test_file.read_text())with pytest.raises(ConflictError) as exc_info:await service.resources.add_resource(ctx=ctx,- path=str(watch_test_file),+ path=str(conflicting_file),to=to_uri,watch_interval=60.0,)@@ -357,7 +361,11 @@ class TestWatchE2EErrorHandling:class MockResourceProcessor:async def process_resource(self, **kwargs):- return {"root_uri": kwargs.get("to", "viking://resources/test")}+ root_uri = kwargs.get("to", "viking://resources/test")+ return {"root_uri": root_uri, "_post_process": {"root_uri": root_uri}}++ async def finish_prepared_resource(self, *_args, **_kwargs):+ return {}class MockSkillProcessor:async def process_skill(self, **kwargs):diff --git a/tests/observability/test_http_error_context.py b/tests/observability/test_http_error_context.pyindex 4b7256de9..3fe6585a0 100644--- a/tests/observability/test_http_error_context.py+++ b/tests/observability/test_http_error_context.py@@ -23,6 +23,8 @@ def test_public_http_error_is_bounded_and_redacts_credentials() -> None:),details={"api_key": "sk-secret",+ "app_secret": "app-secret",+ "custom_secret": "custom-secret","rejected_value": "Bearer another-secret","nested": {"client-secret": "very-secret",@@ -38,6 +40,8 @@ def test_public_http_error_is_bounded_and_redacts_credentials() -> None:assert "bearer-secret" not in error.messageassert error.details == {"api_key": "[REDACTED]",+ "app_secret": "[REDACTED]",+ "custom_secret": "[REDACTED]","rejected_value": "Bearer [REDACTED]","nested": {"client-secret": "[REDACTED]",diff --git a/tests/parse/test_feishu_parser_api.py b/tests/parse/test_feishu_parser_api.pyindex 096288295..ce46242d9 100644--- a/tests/parse/test_feishu_parser_api.py+++ b/tests/parse/test_feishu_parser_api.py@@ -483,17 +483,19 @@ def test_add_resource_message_round_trips_processing_mode():@pytest.mark.asyncio@pytest.mark.parametrize(- ("source", "preflight_name"),+ ("source", "preflight_name", "internal_task"),[- ("https://example.larkoffice.com/docx/doxcnToken", None),- ("https://example.larkoffice.com/sheets/shtcnToken", "Sheet Title"),- ("https://example.larkoffice.com/base/appToken?table=tblSales", "tblSales"),+ ("https://example.larkoffice.com/docx/doxcnToken", None, False),+ ("https://example.larkoffice.com/sheets/shtcnToken", "Sheet Title", False),+ ("https://example.larkoffice.com/base/appToken?table=tblSales", "tblSales", False),+ ("https://example.larkoffice.com/docx/doxcnToken", None, True),],)async def test_uat_producer_payload_reaches_worker_without_persisting_token(monkeypatch,source,preflight_name,+ internal_task,):root_uri = "viking://resources/lark/doxcnToken"submit_understanding = AsyncMock(return_value="response-1")@@ -564,16 +566,18 @@ async def test_uat_producer_payload_reaches_worker_without_persisting_token(wait=False,allow_local_path_resolution=False,args={"feishu_access_token": "u-secret", "custom_option": "forwarded"},+ internal_task=internal_task,)- expected_initial = {"status": "success", "task_id": "task-1"}+ expected_initial = {"status": "success", "task_id": "task-1", "source_path": source}if preflight_name:expected_initial["root_uri"] = root_uriassert initial_result == expected_initialassert task_tracker.create.await_args.kwargs["resource_id"] == (None if preflight_name is None else root_uri)- assert task_tracker.create.await_args.kwargs["meta"] == {"source_path": source}+ expected_meta = {"internal": True} if internal_task else {"source_path": source}+ assert task_tracker.create.await_args.kwargs["meta"] == expected_metasubmit_understanding.assert_awaited_once_with(source,feishu_access_token="u-secret",@@ -1021,6 +1025,7 @@ async def test_add_resource_processor_persists_final_uri_and_cleans_staged_sourcmsg = AddResourceMsg(task_id="task-1",path="https://example.larkoffice.com/docx/doxcnToken",+ source_path="https://storage.example/document.md?X-Signature=secret",root_uri="viking://resources/lark/doxcnToken",account_id="account-1",user_id="user-1",@@ -1055,7 +1060,7 @@ async def test_add_resource_processor_persists_final_uri_and_cleans_staged_sourcaccount_id="account-1",user_id="user-1",task_id="task-1",- meta={"source_path": "", "internal": True},+ meta={"internal": True},)assert task_tracker.complete.await_count == 2first_complete = task_tracker.complete.await_args_list[0]diff --git a/tests/resource/test_feishu_watch_auth.py b/tests/resource/test_feishu_watch_auth.pyindex 8a4102b4d..6c9084c54 100644--- a/tests/resource/test_feishu_watch_auth.py+++ b/tests/resource/test_feishu_watch_auth.py@@ -2,7 +2,13 @@# SPDX-License-Identifier: AGPL-3.0"""Tests for Feishu watch auth helpers."""+import builtinsfrom datetime import datetime, timedelta, timezone+from types import SimpleNamespace+from unittest.mock import MagicMock++import httpx+import pytestfrom openviking.resource import feishu_watch_authfrom openviking.resource.feishu_watch_auth import (@@ -58,6 +64,101 @@ def test_feishu_auth_state_refresh_window():assert feishu_auth_state_needs_refresh(near_expiry, now=now) is True+ ("domain", "token_url"),+ [+ ("https://open.feishu.cn", "https://accounts.feishu.cn/oauth/v3/token"),+ ("https://open.larksuite.com", "https://accounts.larksuite.com/oauth/v3/token"),+ ],+)+def test_refresh_user_token_uses_oauth_v3_and_rotates_refresh_token(monkeypatch, domain, token_url):+ def fake_post(url, *, data, timeout):+ assert url == token_url+ assert data == {+ "grant_type": "refresh_token",+ "client_id": "cli-test",+ "client_secret": "secret-test",+ "refresh_token": "header.payload.signature",+ }+ assert timeout == 12+ return httpx.Response(+ 200,+ json={+ "access_token": "u-new",+ "refresh_token": "r-new",+ "expires_in": 7200,+ },+ )++ monkeypatch.setattr(httpx, "post", fake_post)+ client = FeishuOAuthClient(+ FeishuAppCredentials(+ app_id="cli-test",+ app_secret="secret-test",+ domain=domain,+ request_timeout=12,+ )+ )+ legacy_client = MagicMock()+ legacy_client.authen.v1.refresh_access_token.create.side_effect = AssertionError(+ "legacy authen/v1 refresh endpoint must not be used"+ )+ monkeypatch.setattr(client, "_get_client", lambda: legacy_client, raising=False)++ refreshed = client._refresh_user_access_token_sync("header.payload.signature")++ assert refreshed == FeishuRefreshedToken(+ access_token="u-new",+ refresh_token="r-new",+ expires_in=7200,+ )+++def test_refresh_user_token_keeps_legacy_endpoint_for_ur_tokens(monkeypatch):+ response = MagicMock()+ response.success.return_value = True+ response.data.access_token = "u-new"+ response.data.refresh_token = "ur-new"+ response.data.expires_in = 7200+ legacy_client = MagicMock()+ legacy_client.authen.v1.refresh_access_token.create.return_value = response+ legacy_authen = SimpleNamespace(+ CreateRefreshAccessTokenRequest=MagicMock(),+ CreateRefreshAccessTokenRequestBody=MagicMock(),+ )+ real_import = builtins.__import__++ def fake_import(name, *args, **kwargs):+ if name == "lark_oapi.api.authen.v1":+ return legacy_authen+ return real_import(name, *args, **kwargs)++ monkeypatch.setattr(builtins, "__import__", fake_import)+ monkeypatch.setattr(+ httpx,+ "post",+ MagicMock(side_effect=AssertionError("legacy tokens must not use OAuth v3")),+ )+ client = FeishuOAuthClient(+ FeishuAppCredentials(+ app_id="cli-test",+ app_secret="secret-test",+ domain="https://open.feishu.cn",+ request_timeout=12,+ )+ )+ monkeypatch.setattr(client, "_get_client", lambda: legacy_client, raising=False)++ refreshed = client._refresh_user_access_token_sync("ur-old")++ legacy_client.authen.v1.refresh_access_token.create.assert_called_once()+ assert refreshed == FeishuRefreshedToken(+ access_token="u-new",+ refresh_token="ur-new",+ expires_in=7200,+ )++def test_get_tenant_access_token_uses_configured_app(monkeypatch):seen = {}diff --git a/tests/resource/test_watch_manager.py b/tests/resource/test_watch_manager.pyindex 937fb8bed..eae2ce1bb 100644--- a/tests/resource/test_watch_manager.py+++ b/tests/resource/test_watch_manager.py@@ -248,6 +248,29 @@ class TestWatchManager:assert task.is_active is Trueassert task.next_execution_time is not None+ @pytest.mark.asyncio+ async def test_create_inactive_task_has_no_next_execution(self, watch_manager: WatchManager):+ task = await watch_manager.create_task(+ path="/test/path",+ to_uri="viking://resources/paused",+ watch_interval=30.0,+ is_active=False,+ )++ assert task.is_active is False+ assert task.next_execution_time is None+ assert task in await watch_manager.get_all_tasks(+ TEST_ACCOUNT_ID,+ TEST_USER_ID,+ TEST_ROLE,+ )+ assert task not in await watch_manager.get_all_tasks(+ TEST_ACCOUNT_ID,+ TEST_USER_ID,+ TEST_ROLE,+ active_only=True,+ )+@pytest.mark.asyncioasync def test_deactivate_tasks_under_uri_internal_matches_subtree(self, watch_manager_no_fs: WatchManager@@ -356,6 +379,58 @@ class TestWatchManager:assert loaded.auth_state == task.auth_stateassert "auth_state" not in loaded.to_dict()+ @pytest.mark.asyncio+ async def test_connector_states_persisted_and_hidden_from_public_dict(+ self, mock_viking_fs: MockVikingFS+ ):+ manager1 = WatchManager(viking_fs=mock_viking_fs)+ await manager1.initialize()+ states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00+00:00"}}}+ task = await manager1.create_task(+ path="tos://bucket/docs/",+ source_type="tos",+ to_uri="viking://resources/imports",+ watch_interval=30.0,+ auth_state={"provider": "connector_encrypted", "ciphertext": "encrypted"},+ connector_states=states,+ )++ manager2 = WatchManager(viking_fs=mock_viking_fs)+ await manager2.initialize()+ loaded = await manager2.get_task(task.task_id)++ assert loaded is not None+ assert loaded.connector_states == states+ assert "connector_states" not in loaded.to_dict()++ @pytest.mark.asyncio+ async def test_execution_result_is_persisted_and_public(self, mock_viking_fs: MockVikingFS):+ manager1 = WatchManager(viking_fs=mock_viking_fs)+ await manager1.initialize()+ task = await manager1.create_task(+ path="tos://bucket/docs/",+ to_uri="viking://resources/imports",+ watch_interval=30.0,+ )++ await manager1.record_execution(+ task.task_id,+ status="failed",+ execution_task_id="ingest-task-1",+ error="pull failed with Bearer secret-token",+ )++ manager2 = WatchManager(viking_fs=mock_viking_fs)+ await manager2.initialize()+ loaded = await manager2.get_task(task.task_id)++ assert loaded is not None+ assert loaded.last_task_id == "ingest-task-1"+ assert loaded.last_status == "failed"+ assert loaded.last_error == "pull failed with Bearer [REDACTED]"+ assert loaded.last_execution_time is not None+ assert loaded.to_dict()["last_error"] == loaded.last_error+@pytest.mark.asyncioasync def test_create_task_without_path_raises(self, watch_manager: WatchManager):"""Test that creating a task without path raises error."""@@ -502,6 +577,7 @@ class TestWatchManager:)assert len(tasks) == 3+ assert [task.path for task in tasks] == ["/test/path3", "/test/path2", "/test/path1"]@pytest.mark.asyncioasync def test_get_all_tasks_active_only(self, watch_manager: WatchManager):diff --git a/tests/resource/test_watch_scheduler.py b/tests/resource/test_watch_scheduler.pyindex a2f75c928..a742786d4 100644--- a/tests/resource/test_watch_scheduler.py+++ b/tests/resource/test_watch_scheduler.py@@ -1,4 +1,5 @@import asyncio+from datetime import datetime, timedeltafrom unittest.mock import AsyncMockimport pytest@@ -21,6 +22,104 @@ class TestWatchSchedulerValidation:with pytest.raises(ValueError, match="max_concurrency must be > 0"):WatchScheduler(resource_service=rs, max_concurrency=0)+ def test_task_timeout_must_be_positive(self):+ rs = ResourceService()+ with pytest.raises(ValueError, match="task_timeout must be > 0"):+ WatchScheduler(resource_service=rs, task_timeout=0)+++class TestWatchSchedulerExecutionHold:+ @pytest.mark.asyncio+ async def test_held_due_task_is_skipped_until_released(self):+ class FakeResourceService(ResourceService):+ def __init__(self):+ super().__init__()+ self.calls = []++ async def refresh_resource(self, **kwargs):+ self.calls.append(kwargs)+ return {"root_uri": kwargs.get("to")}++ resource_service = FakeResourceService()+ scheduler = WatchScheduler(resource_service=resource_service, check_interval=1)+ manager = WatchManager(viking_fs=None)+ await manager.initialize()+ scheduler._watch_manager = manager+ task = await manager.create_task(+ path="https://example.com/doc",+ to_uri="viking://resources/doc",+ watch_interval=5,+ )+ task.next_execution_time = datetime.now() - timedelta(minutes=1)++ assert await scheduler.hold_execution(task.task_id) is True+ await scheduler._check_and_execute_due_tasks()+ assert resource_service.calls == []++ await scheduler.release_execution(task.task_id)+ await scheduler._check_and_execute_due_tasks()+ await asyncio.gather(*list(scheduler._execution_tasks))+ assert len(resource_service.calls) == 1+ assert scheduler._executing_tasks == set()++ @pytest.mark.asyncio+ async def test_stuck_ingestion_does_not_block_later_scheduler_passes(self, monkeypatch):+ second_started = asyncio.Event()+ cancelled = asyncio.Event()++ class FakeResourceService(ResourceService):+ async def refresh_resource(self, **kwargs):+ if kwargs["path"].endswith("stuck"):+ return {"status": "success", "task_id": "stuck-ingestion"}+ second_started.set()+ return {"status": "completed"}++ class FakeTracker:+ async def wait(self, *_args, timeout=None, **_kwargs):+ await asyncio.wait_for(asyncio.Event().wait(), timeout=timeout)++ async def cancel(self, *_args, **_kwargs):+ cancelled.set()++ monkeypatch.setattr(+ "openviking.service.task_tracker.get_task_tracker",+ lambda: FakeTracker(),+ )+ scheduler = WatchScheduler(+ resource_service=FakeResourceService(),+ check_interval=1,+ max_concurrency=1,+ task_timeout=0.01,+ )+ manager = WatchManager(viking_fs=None)+ await manager.initialize()+ scheduler._watch_manager = manager++ stuck = await manager.create_task(+ path="https://example.com/stuck",+ to_uri="viking://resources/stuck",+ watch_interval=5,+ )+ stuck.next_execution_time = datetime.now() - timedelta(minutes=1)+ await asyncio.wait_for(scheduler._check_and_execute_due_tasks(), timeout=1)+ await asyncio.sleep(0)++ later = await manager.create_task(+ path="https://example.com/later",+ to_uri="viking://resources/later",+ watch_interval=5,+ )+ later.next_execution_time = datetime.now() - timedelta(minutes=1)+ await asyncio.wait_for(scheduler._check_and_execute_due_tasks(), timeout=1)+ await asyncio.wait_for(second_started.wait(), timeout=1)+ await asyncio.wait_for(cancelled.wait(), timeout=1)+ await asyncio.gather(*list(scheduler._execution_tasks))++ updated = await manager.get_task(stuck.task_id)+ assert updated is not None+ assert updated.last_status == "failed"+ assert updated.last_error == "ingestion task timed out after 0.01s"+class TestWatchSchedulerResourceExistence:def test_url_like_sources_are_treated_as_existing(self):@@ -71,6 +170,8 @@ class TestWatchSchedulerResourceExistence:updated = await manager.get_task(task.task_id)assert updated is not Noneassert updated.is_active is False+ assert updated.last_status == "failed"+ assert updated.last_error == f"Watched target URI does not exist: {task.to_uri}"assert resource_service.calls == []@pytest.mark.asyncio@@ -99,7 +200,6 @@ class TestWatchSchedulerResourceExistence:manager = WatchManager(viking_fs=None)await manager.initialize()scheduler._watch_manager = manager- manager.update_execution_time = AsyncMock()task = await manager.create_task(path=str(source),to_uri="viking://resources/codeask/wiki",@@ -112,7 +212,8 @@ class TestWatchSchedulerResourceExistence:assert updated is not Noneassert updated.is_active is Trueassert resource_service.calls and resource_service.calls[0]["to"] == task.to_uri- manager.update_execution_time.assert_awaited_once_with(task.task_id)+ assert updated.last_status == "completed"+ assert updated.last_execution_time is not None@pytest.mark.asyncioasync def test_execute_task_uses_stable_target_and_options(self, tmp_path):@@ -137,7 +238,6 @@ class TestWatchSchedulerResourceExistence:manager = WatchManager(uri_mutation_coordinator=coordinator)await manager.initialize()scheduler._watch_manager = manager- manager.update_execution_time = AsyncMock()old_uri = "viking://resources/codeask/wiki"new_uri = "viking://resources/codeask/wiki-renamed"task = await manager.create_task(@@ -165,3 +265,82 @@ class TestWatchSchedulerResourceExistence:assert resource_service.calls[0]["enforce_public_remote_targets"] is Trueassert resource_service.calls[0]["ctx"].role == Role.USERassert resource_service.calls[0]["ctx"].bypass_acl is True+ assert "connector_states" not in resource_service.calls[0]++ @pytest.mark.asyncio+ async def test_connector_failure_is_recorded_on_watch(self):+ class FakeResourceService(ResourceService):+ async def refresh_resource(self, **kwargs):+ return {+ "status": "failed",+ "task_id": "connector-task-1",+ "error": "connector task failed: TOS pull failed",+ }++ resource_service = FakeResourceService()+ scheduler = WatchScheduler(resource_service=resource_service, check_interval=1)+ manager = WatchManager(viking_fs=None)+ await manager.initialize()+ scheduler._watch_manager = manager+ task = await manager.create_task(+ path="tos://bucket/docs/",+ to_uri="viking://resources/imports",+ watch_interval=30.0,+ auth_state={"provider": "connector_encrypted", "ciphertext": "encrypted"},+ )+ resource_service._connector.restore_watch_request = AsyncMock(+ return_value=("api-key", "tos", {})+ )++ await scheduler._execute_task(task)++ updated = await manager.get_task(task.task_id)+ assert updated is not None+ assert updated.last_task_id == "connector-task-1"+ assert updated.last_status == "failed"+ assert updated.last_error == "connector task failed: TOS pull failed"++ @pytest.mark.asyncio+ async def test_native_import_failure_is_recorded_on_watch(self, tmp_path, monkeypatch):+ class FakeResourceService(ResourceService):+ async def refresh_resource(self, **kwargs):+ return {"status": "success", "task_id": "add-resource-task-1"}++ class FailedStatus:+ value = "failed"++ class FailedTask:+ status = FailedStatus()+ error = "document import failed"++ tracker = AsyncMock()+ tracker.wait.return_value = FailedTask()+ monkeypatch.setattr(+ "openviking.service.task_tracker.get_task_tracker",+ lambda: tracker,+ )+ source = tmp_path / "source.txt"+ source.write_text("ok")+ scheduler = WatchScheduler(resource_service=FakeResourceService(), check_interval=1)+ manager = WatchManager(viking_fs=None)+ await manager.initialize()+ scheduler._watch_manager = manager+ task = await manager.create_task(+ path=str(source),+ to_uri="viking://resources/imports/source.txt",+ watch_interval=30.0,+ )++ await scheduler._execute_task(task)++ tracker.wait.assert_awaited_once_with(+ "add-resource-task-1",+ account_id=task.account_id,+ user_id=task.user_id,+ timeout=10800,+ )+ updated = await manager.get_task(task.task_id)+ assert updated is not None+ assert updated.last_task_id == "add-resource-task-1"+ assert updated.last_status == "failed"+ assert updated.last_error == "document import failed"diff --git a/tests/server/test_api_resources.py b/tests/server/test_api_resources.pyindex 9b566dd5c..1bd330753 100644--- a/tests/server/test_api_resources.py+++ b/tests/server/test_api_resources.py@@ -30,6 +30,7 @@ def test_add_resource_request_defaults_processing_mode():request = AddResourceRequest(path="https://example.com/demo.md")assert request.processing_mode == "semantic_and_vectors"+ assert request.is_active is Truedef test_add_resource_request_accepts_declared_add_type():@@ -43,6 +44,40 @@ def test_add_resource_request_accepts_declared_add_type():assert request.to == "viking://resources/feishu"+ "target",+ [+ {"to": "viking://resources/docs"},+ {"parent": "viking://resources"},+ ],+)+def test_add_resource_request_accepts_paused_watch_with_target(target):+ request = AddResourceRequest(+ path="https://example.feishu.cn/docx/doc_token",+ watch_interval=30,+ is_active=False,+ **target,+ )++ assert request.is_active is False+++ "kwargs",+ [+ {"watch_interval": 0, "to": "viking://resources/docs"},+ {"watch_interval": 30},+ ],+)+def test_add_resource_request_rejects_invalid_paused_watch(kwargs):+ with pytest.raises(ValueError, match="is_active=false"):+ AddResourceRequest(+ path="tos://bucket/docs/",+ is_active=False,+ **kwargs,+ )++def test_add_resource_request_rejects_add_type_with_temp_file_id():import pytest@@ -209,7 +244,35 @@ async def test_add_resource_forwards_args_to_service(assert seen["internal_task"] is False-async def test_add_resource_marks_connector_child_task_internal(+async def test_add_resource_forwards_paused_watch_to_service(+ client: httpx.AsyncClient,+ service,+ monkeypatch,+):+ seen = {}++ async def fake_add_resource(**kwargs):+ seen.update(kwargs)+ return {"status": "accepted", "task_id": "task-1"}++ monkeypatch.setattr(service.resources, "add_resource", fake_add_resource)++ resp = await client.post(+ "/api/v1/resources",+ json={+ "path": "tos://bucket/docs/",+ "to": "viking://resources/docs",+ "watch_interval": 30,+ "is_active": False,+ },+ )++ assert resp.status_code == 200+ assert seen["watch_interval"] == 30+ assert seen["is_active"] is False+++async def test_add_resource_forwards_internal_task_to_service(client: httpx.AsyncClient,service,monkeypatch,@@ -224,8 +287,7 @@ async def test_add_resource_marks_connector_child_task_internal(resp = await client.post("/api/v1/resources",- headers={"X-OpenViking-Task-Origin": "connector_import"},- json={"path": "https://example.com/demo.md"},+ json={"path": "https://example.com/demo.md", "internal_task": True},)assert resp.status_code == 200@@ -1110,10 +1172,6 @@ async def test_add_resource_non_wait_queue_task_queryable(sample_markdown_file,upload_temp_dir,):- from openviking.service.task_tracker import set_task_tracker-- set_task_tracker(None)-resp = await client.post("/api/v1/resources",json={diff --git a/tests/server/test_api_watches.py b/tests/server/test_api_watches.pyindex 8d770831e..f03f5e598 100644--- a/tests/server/test_api_watches.py+++ b/tests/server/test_api_watches.py@@ -119,6 +119,24 @@ async def test_full_lifecycle(client: httpx.AsyncClient, watch_manager, monkeypaassert resp.status_code == 404+async def test_list_exposes_last_execution_result(client: httpx.AsyncClient, watch_manager):+ task = await _seed(watch_manager, to_uri="viking://resources/test/observable")+ await watch_manager.record_execution(+ task.task_id,+ status="failed",+ execution_task_id="connector-task-1",+ error="pull failed",+ )++ resp = await client.get("/api/v1/watches")++ assert resp.status_code == 200+ result = resp.json()["result"]["tasks"][0]+ assert result["last_task_id"] == "connector-task-1"+ assert result["last_status"] == "failed"+ assert result["last_error"] == "pull failed"++async def test_get_by_uri_returns_single_object(client: httpx.AsyncClient, watch_manager):task = await _seed(watch_manager, to_uri="viking://resources/test/uri-keyed")resp = await client.get("/api/v1/watches", params={"to_uri": task.to_uri})diff --git a/tests/service/test_resource_service_connector.py b/tests/service/test_resource_service_connector.pyindex 901433627..296caf072 100644--- a/tests/service/test_resource_service_connector.py+++ b/tests/service/test_resource_service_connector.py@@ -5,6 +5,7 @@import asyncioimport jsonimport logging+from datetime import datetime, timedeltafrom types import SimpleNamespacefrom unittest.mock import AsyncMock, Mock@@ -105,6 +106,7 @@ def _task_tracker():update_stage=AsyncMock(),complete=AsyncMock(),fail=AsyncMock(),+ mark_cancelled=AsyncMock(),)@@ -152,6 +154,8 @@ async def test_add_resource_routes_tos_to_connector(submit_doc_add=AsyncMock(return_value={"task_key": "connector-1"}))_install_connector_dependencies(monkeypatch, tracker, connector_client)+ info_log = Mock()+ monkeypatch.setattr(connector_delegate_module.logger, "info", info_log)result = await service.add_resource(path=path,@@ -181,6 +185,15 @@ async def test_add_resource_routes_tos_to_connector(account_id="acct",user_id="alice",)+ request_log = next(+ call for call in info_log.call_args_list if "task add request" in call.args[0]+ )+ response_log = next(+ call for call in info_log.call_args_list if "task add response" in call.args[0]+ )+ assert request_log.args[2]["api_key"] == "[REDACTED]"+ assert response_log.args[2] == {"task_key": "connector-1"}+ assert "secret" not in str(info_log.call_args_list)@pytest.mark.asyncio@@ -228,19 +241,6 @@ async def test_connector_watch_stores_only_encrypted_replay_state(args={"tos_prefix": ["tos://bucket/docs/"]},)- assert (- await watch_manager.get_task_by_uri(- "viking://resources/x/y",- account_id="acct",- user_id="alice",- role=str(Role.USER),- )- is None- )- background_tasks = list(service._background_tasks)- release_monitor.set()- await asyncio.gather(*background_tasks)-task = await watch_manager.get_task_by_uri("viking://resources/x/y",account_id="acct",@@ -248,6 +248,14 @@ async def test_connector_watch_stores_only_encrypted_replay_state(role=str(Role.USER),)assert task is not None+ assert task.is_active is True+ assert task.last_status is None+ background_tasks = list(service._background_tasks)+ release_monitor.set()+ await asyncio.gather(*background_tasks)++ assert task.last_status == "completed"+ assert task.last_task_id == "task-1"assert task.source_type == "tos"assert task.auth_state["provider"] == "connector_encrypted"assert "secret" not in json.dumps(task.auth_state)@@ -263,6 +271,177 @@ async def test_connector_watch_stores_only_encrypted_replay_state()+ ("task_info", "expected_status", "expected_error"),+ [+ (+ {+ "Status": "succeeded",+ "StreamStates": {"documents": {"cursor": {"page_token": "next"}}},+ },+ "completed",+ None,+ ),+ (+ {"Status": "failed", "ErrorMessage": "source unavailable"},+ "failed",+ "connector task failed: source unavailable",+ ),+ ],+)+async def test_paused_connector_watch_is_visible_before_import_finishes(+ monkeypatch,+ connector_config,+ ctx,+ task_info,+ expected_status,+ expected_error,+):+ tracker = _task_tracker()+ release_import = asyncio.Event()++ async def get_task_info(_task_key, _api_key):+ await release_import.wait()+ return task_info++ connector_client = SimpleNamespace(+ submit_doc_add=AsyncMock(return_value={"task_key": "connector-1"}),+ get_task_info=AsyncMock(side_effect=get_task_info),+ )+ _install_connector_dependencies(+ monkeypatch,+ tracker,+ connector_client,+ discard_monitor=False,+ )+ watch_manager = WatchManager()+ service = ResourceService(+ vikingdb=object(),+ viking_fs=SimpleNamespace(+ exists=AsyncMock(return_value=True),+ _encryptor=_FakeEncryptor(),+ ),+ resource_processor=object(),+ skill_processor=object(),+ watch_scheduler=SimpleNamespace(watch_manager=watch_manager),+ )+ to_uri = "viking://resources/x/y"++ result = await service.add_resource(+ path="tos://bucket/docs/",+ ctx=ctx,+ to=to_uri,+ watch_interval=5,+ is_active=False,+ )++ task = await watch_manager.get_task_by_uri(+ to_uri,+ account_id="acct",+ user_id="alice",+ role=str(Role.USER),+ )+ assert task is not None+ assert task.is_active is False+ assert task.next_execution_time is None+ assert task.last_status is None++ release_import.set()+ await asyncio.gather(*list(service._background_tasks))++ assert task.is_active is False+ assert task.last_task_id == result["task_id"]+ assert task.last_status == expected_status+ assert task.last_error == expected_error+ if expected_status == "completed":+ assert task.connector_states == task_info["StreamStates"]+ else:+ assert task.connector_states is None+++async def test_paused_watch_option_rejects_non_connector_source(+ connector_config,+ ctx,+ service,+):+ with pytest.raises(InvalidArgumentError, match="only supported for Connector"):+ await service.add_resource(+ path="https://example.com/docs.md",+ ctx=ctx,+ to="viking://resources/docs",+ watch_interval=5,+ is_active=False,+ )+++async def test_paused_connector_watch_keeps_submission_failure(+ connector_config,+ ctx,+ service,+):+ watch_manager = WatchManager()+ service._watch_scheduler = SimpleNamespace(watch_manager=watch_manager)+ service._connector.submit = AsyncMock(side_effect=RuntimeError("submission unavailable"))+ to_uri = "viking://resources/docs"++ with pytest.raises(RuntimeError, match="submission unavailable"):+ await service.add_resource(+ path="tos://bucket/docs/",+ ctx=ctx,+ to=to_uri,+ watch_interval=5,+ is_active=False,+ )++ task = await watch_manager.get_task_by_uri(+ to_uri,+ account_id="acct",+ user_id="alice",+ role=str(Role.USER),+ )+ assert task is not None+ assert task.is_active is False+ assert task.last_status == "failed"+ assert task.last_error == "submission unavailable"+++async def test_connector_watch_releases_hold_when_submission_is_cancelled(+ connector_config,+ ctx,+ service,+):+ scheduler = WatchScheduler(resource_service=service, check_interval=1)+ watch_manager = WatchManager()+ scheduler._watch_manager = watch_manager+ service._watch_scheduler = scheduler+ service._connector.create_watch_auth_state = AsyncMock(return_value={"provider": "test"})+ service._connector.submit = AsyncMock(side_effect=asyncio.CancelledError)+ to_uri = "viking://resources/docs"++ with pytest.raises(asyncio.CancelledError):+ await service.add_resource(+ path="tos://bucket/docs/",+ ctx=ctx,+ to=to_uri,+ watch_interval=5,+ )++ task = await watch_manager.get_task_by_uri(+ to_uri,+ account_id="acct",+ user_id="alice",+ role=str(Role.USER),+ )+ assert task is not None+ assert task.last_status == "failed"+ assert task.last_error == "connector task submission cancelled"+ assert scheduler._executing_tasks == set()++@pytest.mark.asyncioasync def test_connector_watch_prechecks_only_active_conflicts(monkeypatch,@@ -333,16 +512,19 @@ async def test_connector_watch_prechecks_only_active_conflicts(watch_interval=5,)- assert existing.is_active is False- assert existing.path == "tos://bucket/old/"+ # A paused watch on the same target is reactivated at submission, not after+ # the import finishes.+ assert existing.is_active is True+ assert existing.path == "tos://bucket/new/"+ assert existing.last_status is Nonerelease_monitor.set()await asyncio.gather(*list(service._background_tasks))assert existing.is_active is True- assert existing.path == "tos://bucket/new/"+ assert existing.last_status == "completed"@pytest.mark.asyncio-async def test_connector_watch_resolves_overlapping_imports_at_finalize(+async def test_connector_watch_rejects_overlapping_imports_at_submit(connector_config,ctx,):@@ -357,13 +539,7 @@ async def test_connector_watch_resolves_overlapping_imports_at_finalize(skill_processor=object(),watch_scheduler=SimpleNamespace(watch_manager=watch_manager),)- finalizers = []-- async def submit(**kwargs):- finalizers.append(kwargs["on_success"])- return {"status": "accepted"}-- service._connector.submit = AsyncMock(side_effect=submit)+ service._connector.submit = AsyncMock(return_value={"status": "accepted"})to_uri = "viking://resources/x/y"await service.add_resource(@@ -372,17 +548,114 @@ async def test_connector_watch_resolves_overlapping_imports_at_finalize(to=to_uri,watch_interval=5,)+ with pytest.raises(ConflictError, match="already being monitored"):+ await service.add_resource(+ path="tos://bucket/second/",+ ctx=ctx,+ to=to_uri,+ watch_interval=5,+ )++ # The second import never reaches the Connector, so nothing overlapping is written.+ assert service._connector.submit.await_count == 1+ task = await watch_manager.get_task_by_uri(+ to_uri,+ account_id="acct",+ user_id="alice",+ role=str(Role.USER),+ )+ assert task is not None+ assert task.path == "tos://bucket/first/"+++async def test_connector_watch_rejects_same_source_while_first_import_is_running(+ connector_config,+ ctx,+ service,+):+ scheduler = WatchScheduler(resource_service=service, check_interval=1)+ watch_manager = WatchManager()+ scheduler._watch_manager = watch_manager+ service._watch_scheduler = scheduler+ service._connector.create_watch_auth_state = AsyncMock(return_value={"provider": "test"})+ service._connector.submit = AsyncMock(return_value={"status": "accepted"})+ to_uri = "viking://resources/x/y"+ path = "tos://bucket/docs/"+await service.add_resource(- path="tos://bucket/second/",+ path=path,ctx=ctx,to=to_uri,+ reason="first",watch_interval=5,)+ with pytest.raises(ConflictError, match="already executing"):+ await service.add_resource(+ path=path,+ ctx=ctx,+ to=to_uri,+ reason="second",+ watch_interval=5,+ )- assert service._connector.submit.await_count == 2- await finalizers[0]()- with pytest.raises(ConflictError, match="already being monitored"):- await finalizers[1]()+ task = await watch_manager.get_task_by_uri(+ to_uri,+ account_id="acct",+ user_id="alice",+ role=str(Role.USER),+ )+ assert task is not None+ assert task.reason == "first"+ assert service._connector.submit.await_count == 1+ assert scheduler._executing_tasks == {task.task_id}+++async def test_active_connector_watch_is_visible_and_held_until_first_round_ends(+ monkeypatch,+ connector_config,+ ctx,+):+ tracker = _task_tracker()+ release_import = asyncio.Event()++ async def get_task_info(_task_key, _api_key):+ await release_import.wait()+ return {"Status": "succeeded", "StreamStates": {"documents": {"cursor": {"p": 1}}}}++ connector_client = SimpleNamespace(+ submit_doc_add=AsyncMock(return_value={"task_key": "connector-1"}),+ get_task_info=AsyncMock(side_effect=get_task_info),+ )+ _install_connector_dependencies(+ monkeypatch,+ tracker,+ connector_client,+ discard_monitor=False,+ )+ service = ResourceService(+ vikingdb=object(),+ viking_fs=SimpleNamespace(+ exists=AsyncMock(return_value=True),+ _encryptor=_FakeEncryptor(),+ ),+ resource_processor=object(),+ skill_processor=object(),+ )+ service.refresh_resource = AsyncMock(return_value={"status": "completed"})+ scheduler = WatchScheduler(resource_service=service, check_interval=1)+ watch_manager = WatchManager()+ scheduler._watch_manager = watch_manager+ service._watch_scheduler = scheduler+ to_uri = "viking://resources/x/y"++ result = await service.add_resource(+ path="tos://bucket/docs/",+ ctx=ctx,+ to=to_uri,+ watch_interval=5,+ )task = await watch_manager.get_task_by_uri(to_uri,@@ -391,7 +664,24 @@ async def test_connector_watch_resolves_overlapping_imports_at_finalize(role=str(Role.USER),)assert task is not None- assert task.path == "tos://bucket/first/"+ assert task.is_active is True+ assert task.next_execution_time is not None+ assert task.last_status is None+ assert scheduler._executing_tasks == {task.task_id}++ # Even when the watch falls due during the first round, the scheduler skips it.+ task.next_execution_time = datetime.now() - timedelta(minutes=1)+ await scheduler._check_and_execute_due_tasks()+ service.refresh_resource.assert_not_awaited()++ release_import.set()+ await asyncio.gather(*list(service._background_tasks))++ assert scheduler._executing_tasks == set()+ assert task.last_task_id == result["task_id"]+ assert task.last_status == "completed"+ assert task.connector_states == {"documents": {"cursor": {"p": 1}}}+ assert task.next_execution_time > task.last_execution_time@pytest.mark.asyncio@@ -530,7 +820,8 @@ async def test_declared_connector_watch_returns_original_add_type(to="viking://resources/Project/ptat4n",watch_interval=5,)- await submitted["on_success"]()+ states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00+00:00"}}}+ await submitted["on_success"](states)task = await watch_manager.get_task_by_uri("viking://resources/Project/ptat4n",@@ -540,6 +831,7 @@ async def test_declared_connector_watch_returns_original_add_type()assert task is not Noneassert task.source_type == "feishu_project"+ assert task.connector_states == states@pytest.mark.asyncio@@ -553,7 +845,11 @@ async def test_connector_watch_scheduler_restores_request_credentials(resource_processor=object(),skill_processor=object(),)- service.refresh_resource = AsyncMock(return_value={"status": "completed"})+ old_states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00+00:00"}}}+ new_states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-02T00:00:00+00:00"}}}+ service.refresh_resource = AsyncMock(+ return_value={"status": "completed", "connector_states": new_states}+ )scheduler = WatchScheduler(resource_service=service)watch_manager = WatchManager()scheduler._watch_manager = watch_manager@@ -571,6 +867,7 @@ async def test_connector_watch_scheduler_restores_request_credentials(account_id="acct",user_id="alice",auth_state=auth_state,+ connector_states=old_states,)await scheduler._execute_stable_task(task)@@ -579,6 +876,44 @@ async def test_connector_watch_scheduler_restores_request_credentials(assert call.kwargs["ctx"].api_key == "secret"assert call.kwargs["add_type"] == "tos"assert call.kwargs["args"] == {"tos_prefix": ["tos://bucket/docs/"]}+ assert call.kwargs["connector_states"] == old_states+ updated = await watch_manager.get_task(task.task_id)+ assert updated is not None+ assert updated.connector_states == new_states+++async def test_failed_connector_watch_keeps_previous_stream_states(connector_config):+ service = ResourceService()+ service.refresh_resource = AsyncMock(return_value={"status": "failed"})+ scheduler = WatchScheduler(resource_service=service)+ watch_manager = WatchManager()+ scheduler._watch_manager = watch_manager+ old_states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00+00:00"}}}+ task = await watch_manager.create_task(+ path="tos://bucket/docs/",+ to_uri="viking://resources/x/y",+ watch_interval=5,+ account_id="acct",+ user_id="alice",+ auth_state={+ "provider": "connector_plaintext",+ "request": {+ "api_key": "secret",+ "account_id": "acct",+ "add_type": "tos",+ "path": "tos://bucket/docs/",+ "connector_args": {},+ },+ },+ connector_states=old_states,+ )++ await scheduler._execute_stable_task(task)++ updated = await watch_manager.get_task(task.task_id)+ assert updated is not None+ assert updated.connector_states == old_states@pytest.mark.asyncio@@ -619,15 +954,18 @@ async def test_connector_watch_refresh_waits_for_remote_completion():service._connector.submit = AsyncMock(return_value={"status": "completed"})+ states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00+00:00"}}}await service.refresh_resource(path="tos://bucket/docs/",ctx=ctx,to="viking://resources/x/y",watch_interval=5,add_type="tos",+ connector_states=states,)assert service._connector.submit.await_args.kwargs["wait_for_completion"] is True+ assert service._connector.submit.await_args.kwargs["connector_states"] == states@pytest.mark.asyncio@@ -1016,29 +1354,77 @@ async def test_connector_import_persists_task_before_remote_submission(connector_config,ctx,service,+ caplog,):tracker = _task_tracker()async def fail_submission(**_kwargs):tracker.create.assert_awaited_once()- raise RuntimeError("submission failed")+ raise RuntimeError("submission failed: token=secret-value")connector_client = SimpleNamespace(submit_doc_add=AsyncMock(side_effect=fail_submission))_install_connector_dependencies(monkeypatch, tracker, connector_client)- with pytest.raises(RuntimeError, match="submission failed"):+ with caplog.at_level(logging.ERROR):+ with pytest.raises(RuntimeError, match="submission failed"):+ await service.add_resource(+ path="tos://bucket/prefix",+ ctx=ctx,+ to="viking://resources/imports",+ )++ tracker.fail.assert_awaited_once_with(+ "task-1",+ "submission failed: token=[REDACTED]",+ account_id="acct",+ user_id="alice",+ )+ assert "secret-value" not in caplog.text+++async def test_connector_import_logs_http_error_response(+ monkeypatch,+ connector_config,+ ctx,+ service,+):+ tracker = _task_tracker()+ request = httpx.Request("POST", connector_config.connector)+ response = httpx.Response(+ 400,+ request=request,+ json={"message": "invalid path_prefix", "token": "secret-value"},+ )+ connector_client = SimpleNamespace(+ submit_doc_add=AsyncMock(+ side_effect=httpx.HTTPStatusError(+ "bad request",+ request=request,+ response=response,+ )+ )+ )+ _install_connector_dependencies(monkeypatch, tracker, connector_client)+ error_log = Mock()+ monkeypatch.setattr(connector_delegate_module.logger, "error", error_log)++ with pytest.raises(httpx.HTTPStatusError):await service.add_resource(path="tos://bucket/prefix",ctx=ctx,to="viking://resources/imports",)- tracker.fail.assert_awaited_once_with(- "task-1",- "submission failed",- account_id="acct",- user_id="alice",+ response_log = next(+ call for call in error_log.call_args_list if "task add response" in call.args[0])+ assert response_log.args[2] == 400+ assert response_log.args[3] == {+ "message": "invalid path_prefix",+ "token": "[REDACTED]",+ }+ assert "secret-value" not in str(error_log.call_args_list)@pytest.mark.asyncio@@ -1679,6 +2065,7 @@ async def test_add_resource_falls_back_for_shared_source_with_parent("status": "success","task_id": "task-standard","root_uri": "viking://resources/repo/repo",+ "source_path": "https://git.example/org/repo.git",}service._connector.submit.assert_not_awaited()service._enqueue_add_resource_job.assert_awaited_once()@@ -1790,19 +2177,25 @@ async def test_connector_import_without_target_is_rejected(@pytest.mark.asyncio-async def test_connector_import_rejects_target_outside_public_resources_root(+async def test_connector_import_rejects_target_outside_resources_tree(connector_config,ctx,service,):- with pytest.raises(InvalidArgumentError, match="public resources root"):+ with pytest.raises(InvalidArgumentError, match="resources tree"):await service.add_resource(path="tos://bucket/prefix",ctx=ctx,- to="viking://user/alice/resources/spec",+ to="viking://user/alice/memories/spec",)+def test_connector_import_accepts_user_resources_target(connector_config, ctx, service):+ # Same rule as native imports: a user's own resources tree is a valid target.+ for to in ("viking://user/alice/resources/spec", "viking://user/alice/resources"):+ assert service._connector.should_delegate("tos://bucket/prefix", ctx=ctx, to=to)++@pytest.mark.asyncioasync def test_connector_import_rejects_unsupported_args(connector_config,@@ -1930,13 +2323,20 @@ async def test_git_reason_routes_to_connector(@pytest.mark.asyncio@pytest.mark.parametrize(- ("task_info", "expected_stage", "expected_error"),+ ("task_info", "expected_stage", "expected_status", "expected_error"),[- ({"Status": "succeeded"}, "connector:succeeded", None),+ ({"Status": "succeeded"}, "connector:succeeded", "completed", None),(- {"status": "failed", "error_message": "source unavailable"},+ {"status": "failed", "error_message": "source unavailable; token=secret-value"},"connector:failed",- "connector task failed: source unavailable",+ "failed",+ "connector task failed: source unavailable; token=[REDACTED]",+ ),+ (+ {"status": "cancelled", "error_message": "stopped by user"},+ "connector:cancelled",+ "cancelled",+ "connector task cancelled: stopped by user",),],)@@ -1946,7 +2346,9 @@ async def test_monitor_connector_task_maps_terminal_status(ctx,task_info,expected_stage,+ expected_status,expected_error,+ caplog,):tracker = _task_tracker()monkeypatch.setattr(@@ -1961,6 +2363,65 @@ async def test_monitor_connector_task_maps_terminal_status(client = SimpleNamespace(get_task_info=AsyncMock(return_value=task_info))on_success = AsyncMock()+ with caplog.at_level(logging.WARNING):+ outcome = await ResourceService()._connector._monitor(+ client=client,+ connector_task_key="connector-1",+ ov_task_id="task-1",+ poll_interval_ms=1,+ timeout_seconds=1,+ ctx=ctx,+ on_success=on_success,+ )++ assert tracker.update_stage.await_args.args[1] == expected_stage+ assert outcome["status"] == expected_status+ if expected_status == "completed":+ on_success.assert_awaited_once_with(None)+ tracker.complete.assert_awaited_once()+ tracker.fail.assert_not_awaited()+ tracker.mark_cancelled.assert_not_awaited()+ elif expected_status == "cancelled":+ assert outcome == {"status": "cancelled", "error": expected_error}+ on_success.assert_not_awaited()+ tracker.mark_cancelled.assert_awaited_once_with(+ "task-1",+ account_id="acct",+ user_id="alice",+ )+ tracker.fail.assert_not_awaited()+ tracker.complete.assert_not_awaited()+ else:+ assert outcome == {"status": expected_status, "error": expected_error}+ on_success.assert_not_awaited()+ assert tracker.fail.await_args.args[1] == expected_error+ tracker.mark_cancelled.assert_not_awaited()+ tracker.complete.assert_not_awaited()+ assert "secret-value" not in caplog.text+++async def test_monitor_returns_external_connector_stream_states(+ monkeypatch,+ connector_config,+ ctx,+):+ tracker = _task_tracker()+ monkeypatch.setattr(+ "openviking.service.task_tracker.get_task_tracker",+ lambda: tracker,+ )++ async def no_sleep(_seconds):+ pass++ monkeypatch.setattr(connector_delegate_module.asyncio, "sleep", no_sleep)+ states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-02T00:00:00+00:00"}}}+ client = SimpleNamespace(+ get_task_info=AsyncMock(return_value={"Status": "succeeded", "StreamStates": states})+ )+ on_success = AsyncMock()+outcome = await ResourceService()._connector._monitor(client=client,connector_task_key="connector-1",@@ -1971,17 +2432,8 @@ async def test_monitor_connector_task_maps_terminal_status(on_success=on_success,)- assert tracker.update_stage.await_args.args[1] == expected_stage- if expected_error is None:- assert outcome["status"] == "completed"- on_success.assert_awaited_once_with()- tracker.complete.assert_awaited_once()- tracker.fail.assert_not_awaited()- else:- assert outcome == {"status": "failed", "error": expected_error}- on_success.assert_not_awaited()- assert tracker.fail.await_args.args[1] == expected_error- tracker.complete.assert_not_awaited()+ assert outcome["connector_states"] == states+ on_success.assert_awaited_once_with(states)@pytest.mark.asyncio@@ -2026,6 +2478,61 @@ async def test_monitor_connector_task_retries_transient_polling_error(tracker.fail.assert_not_awaited()+async def test_monitor_connector_task_logs_http_error_response(+ monkeypatch,+ connector_config,+ ctx,+):+ tracker = _task_tracker()+ monkeypatch.setattr(+ "openviking.service.task_tracker.get_task_tracker",+ lambda: tracker,+ )++ async def no_sleep(_seconds):+ pass++ monkeypatch.setattr(connector_delegate_module.asyncio, "sleep", no_sleep)+ request = httpx.Request("POST", "https://connector.example/task/info")+ response = httpx.Response(+ 503,+ request=request,+ text='{"message":"token=secret-value unavailable"}',+ )+ client = SimpleNamespace(+ get_task_info=AsyncMock(+ side_effect=[+ httpx.HTTPStatusError(+ "service unavailable",+ request=request,+ response=response,+ ),+ {"Status": "succeeded"},+ ]+ )+ )+ warning_log = Mock()+ monkeypatch.setattr(connector_delegate_module.logger, "warning", warning_log)++ await ResourceService()._connector._monitor(+ client=client,+ connector_task_key="connector-1",+ ov_task_id="task-1",+ poll_interval_ms=1,+ timeout_seconds=1,+ ctx=ctx,+ )++ error_log = next(+ call for call in warning_log.call_args_list if "task info error response" in call.args[0]+ )+ assert error_log.args[3] == 503+ assert error_log.args[4] == {"message": "token=[REDACTED]"}+ assert "secret-value" not in str(warning_log.call_args_list)+ tracker.complete.assert_awaited_once()++@pytest.mark.asyncioasync def test_monitor_connector_task_marks_cancelled_monitor_as_failed(monkeypatch,@@ -2079,6 +2586,8 @@ async def test_declared_add_type_routes_generic_type_to_connector(submit_doc_add=AsyncMock(return_value={"task_key": "connector-1"}))_install_connector_dependencies(monkeypatch, tracker, connector_client)+ info_log = Mock()+ monkeypatch.setattr(connector_delegate_module.logger, "info", info_log)result = await service.add_resource(path="https://example.feishu.cn/wiki/space-home",@@ -2102,6 +2611,11 @@ async def test_declared_add_type_routes_generic_type_to_connector(auth_config={"app_secret": "s3cret"},extra_params=None,)+ request_log = next(+ call for call in info_log.call_args_list if "task add request" in call.args[0]+ )+ assert request_log.args[2]["auth_config"] == "[REDACTED]"+ assert "s3cret" not in str(info_log.call_args_list)@pytest.mark.asynciodiff --git a/tests/service/test_resource_service_watch.py b/tests/service/test_resource_service_watch.pyindex 54dbcda01..5c2666799 100644--- a/tests/service/test_resource_service_watch.py+++ b/tests/service/test_resource_service_watch.py@@ -2,9 +2,10 @@# SPDX-License-Identifier: AGPL-3.0"""Integration tests for ResourceService watch functionality."""+import asynciofrom types import SimpleNamespacefrom typing import AsyncGenerator-from unittest.mock import AsyncMock, MagicMock+from unittest.mock import AsyncMock, MagicMock, Mockimport pytestimport pytest_asyncio@@ -15,8 +16,11 @@ from openviking.server.identity import RequestContext, Rolefrom openviking.service import resource_service as resource_service_modulefrom openviking.service.resource_service import ResourceServicefrom openviking.service.task_tracker import TaskStatus+from openviking.service.task_work_index import TASK_WORK_ID_FIELDfrom openviking.storage.content_write import ContentWriteCoordinatorfrom openviking.storage.queuefs.add_resource_msg import AddResourceMsg+from openviking.storage.queuefs.add_resource_processor import AddResourceProcessor+from openviking.storage.queuefs.queue_manager import QueueManagerfrom openviking.utils.ingest_options import IngestOptionsfrom openviking_cli.exceptions import ConflictError, InvalidArgumentErrorfrom openviking_cli.session.user_id import UserIdentifier@@ -127,6 +131,8 @@ async def resource_service(watch_manager: WatchManager) -> AsyncGenerator[Resour"""Create ResourceService instance with watch support."""scheduler = MagicMock()scheduler.watch_manager = watch_manager+ scheduler.hold_execution = AsyncMock(return_value=True)+ scheduler.release_execution = AsyncMock()service = ResourceService(vikingdb=MockVikingDB(),viking_fs=MockVikingFS(),@@ -535,6 +541,110 @@ class TestAddResourceArgs:}assert "auth_state" not in task.to_dict()+ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ ("target", "is_active"),+ [+ ({"to": "viking://resources/feishu"}, False),+ ({"parent": "viking://resources"}, False),+ ({"to": "viking://resources/feishu"}, True),+ ],+ )+ async def test_native_feishu_watch_is_created_during_queue_processing(+ self,+ monkeypatch: pytest.MonkeyPatch,+ resource_service: ResourceService,+ request_context: RequestContext,+ target,+ is_active,+ ):+ to_uri = "viking://resources/feishu"+ resource_service._plan_source_job_target = AsyncMock(+ return_value=(to_uri, None, False, False)+ )++ async def preflight(_self, _source, *, feishu_access_token=None):+ return SimpleNamespace(source_name="Feishu", source_format="file")++ monkeypatch.setattr(+ "openviking.parse.accessors.feishu_accessor.FeishuAccessor.preflight_source",+ preflight,+ )++ result = await resource_service.add_resource(+ path="https://example.feishu.cn/docx/doc_token",+ ctx=request_context,+ watch_interval=30,+ is_active=is_active,+ **target,+ )++ assert await get_task_by_uri(resource_service, to_uri, request_context) is None+ assert result["task_id"] == "test-task"++ message = resource_service._enqueue_add_resource_job.await_args.args[0]+ assert message.is_active is is_active+ assert message.watch_task_id is None+ assert message.skip_watch_management is False+ assert AddResourceMsg.from_dict(message.to_dict()).is_active is is_active++ await resource_service.execute_add_resource_job(+ message,+ ctx=request_context,+ resource_lock=None,+ stage_callback=AsyncMock(),+ )++ task = await get_task_by_uri(resource_service, to_uri, request_context)+ assert task is not None+ assert message.watch_task_id == task.task_id+ assert task.is_active is (is_active is not False)+ assert (task.next_execution_time is not None) is (is_active is not False)+ assert len(resource_service._resource_processor.calls) == 1++ @pytest.mark.asyncio+ async def test_native_feishu_preflight_failure_does_not_create_watch(+ self,+ monkeypatch: pytest.MonkeyPatch,+ resource_service: ResourceService,+ request_context: RequestContext,+ ):+ to_uri = "viking://resources/failed_feishu"++ async def fail_preflight(_self, _source, *, feishu_access_token=None):+ raise RuntimeError("preflight unavailable")++ monkeypatch.setattr(+ "openviking.parse.accessors.feishu_accessor.FeishuAccessor.preflight_source",+ fail_preflight,+ )++ with pytest.raises(RuntimeError, match="preflight unavailable"):+ await resource_service.add_resource(+ path="https://example.feishu.cn/docx/doc_token",+ ctx=request_context,+ to=to_uri,+ watch_interval=30,+ is_active=False,+ )++ assert await get_task_by_uri(resource_service, to_uri, request_context) is None++ @pytest.mark.asyncio+ async def test_paused_watch_with_parent_rejects_non_feishu_source(+ self,+ resource_service: ResourceService,+ request_context: RequestContext,+ ):+ with pytest.raises(InvalidArgumentError, match="only supported"):+ await resource_service.add_resource(+ path="/test/path",+ ctx=request_context,+ parent="viking://resources",+ watch_interval=30,+ is_active=False,+ )+@pytest.mark.asyncioasync def test_feishu_user_token_watch_rejects_partial_app_credentials(self,@@ -631,6 +741,126 @@ class TestAddResourceArgs:assert "git-secret" not in str(public_task)+ ("result", "error", "expected_status", "expected_error"),+ [+ (+ {"status": "success", "root_uri": "viking://resources/paused_feishu"},+ None,+ "completed",+ None,+ ),+ (None, RuntimeError("native import failed"), "failed", "native import failed"),+ (+ {"status": "error", "errors": ["parse failed", "write failed"]},+ None,+ "failed",+ "parse failed; write failed",+ ),+ (None, asyncio.CancelledError(), "cancelled", None),+ ],+)+async def test_add_resource_processor_records_paused_watch_result(+ monkeypatch: pytest.MonkeyPatch,+ result,+ error,+ expected_status: str,+ expected_error: str | None,+):+ task_tracker = SimpleNamespace(+ create=AsyncMock(return_value=SimpleNamespace(status=TaskStatus.PENDING)),+ start=AsyncMock(),+ update_stage=AsyncMock(),+ complete=AsyncMock(),+ fail=AsyncMock(),+ get_task_auth=AsyncMock(return_value={}),+ wait_for_descendants=AsyncMock(),+ )+ monkeypatch.setattr(+ "openviking.storage.queuefs.add_resource_processor.get_task_tracker",+ Mock(return_value=task_tracker),+ )++ async def execute_add_resource_job(message, **_kwargs):+ message.watch_task_id = "watch-1"+ if error:+ raise error+ return result++ execute = AsyncMock(side_effect=execute_add_resource_job)+ service = SimpleNamespace(+ execute_add_resource_job=execute,+ _link_resource_reason_memory=AsyncMock(),+ record_watch_execution=AsyncMock(),+ )+ processor = AddResourceProcessor(+ service,+ asyncio.get_running_loop(),+ QueueManager.ADD_RESOURCE,+ SimpleNamespace(_async_agfs=SimpleNamespace(pathlock_release=AsyncMock())),+ )+ message = AddResourceMsg(+ task_id="add-resource-1",+ path="https://example.feishu.cn/docx/doc_token",+ root_uri="viking://resources/paused_feishu",+ account_id="account-1",+ user_id="user-1",+ role="user",+ )++ data = message.to_dict()+ data[TASK_WORK_ID_FIELD] = "work-1"+ if isinstance(error, asyncio.CancelledError):+ with pytest.raises(asyncio.CancelledError):+ await processor._process(message, data)+ else:+ await processor._process(message, data)++ service.record_watch_execution.assert_awaited_once_with(+ "watch-1",+ status=expected_status,+ execution_task_id="add-resource-1",+ error=expected_error,+ )+ if expected_status == "completed":+ assert task_tracker.complete.await_count == 1+++async def test_record_watch_execution_redacts_error_in_log(caplog):+ watch_manager = SimpleNamespace(record_execution=AsyncMock())+ scheduler = SimpleNamespace(+ watch_manager=watch_manager,+ release_execution=AsyncMock(),+ )+ service = ResourceService(watch_scheduler=scheduler)+ error = "connector failed: api_key=secret-watch-key, Authorization: Bearer secret-token"++ resource_service_module.logger.addHandler(caplog.handler)+ try:+ with caplog.at_level("INFO", logger=resource_service_module.logger.name):+ await service.record_watch_execution(+ "watch-1",+ status="failed",+ execution_task_id="connector-1",+ error=error,+ )+ finally:+ resource_service_module.logger.removeHandler(caplog.handler)++ assert "secret-watch-key" not in caplog.text+ assert "secret-token" not in caplog.text+ assert "[REDACTED]" in caplog.text+ watch_manager.record_execution.assert_awaited_once_with(+ "watch-1",+ status="failed",+ execution_task_id="connector-1",+ error=error,+ )+ scheduler.release_execution.assert_awaited_once_with("watch-1")++class TestWatchTaskConflict:"""Tests for watch task conflict detection."""diff --git a/tests/test_task_tracker.py b/tests/test_task_tracker.pyindex 7579d9f7e..27fa096a6 100644--- a/tests/test_task_tracker.py+++ b/tests/test_task_tracker.py@@ -242,6 +242,23 @@ async def test_fail_task(tracker: TaskTracker):assert "LLM timeout" in retrieved.error+async def test_mark_externally_cancelled_task_without_enabling_active_cancel(+ tracker: TaskTracker,+):+ task = await tracker.create("connector_import", **_owner_kwargs())+ await tracker.start(task.task_id)++ with pytest.raises(ValueError, match="does not support cancellation"):+ await tracker.cancel(task.task_id, **_owner_kwargs())++ await tracker.mark_cancelled(task.task_id, **_owner_kwargs())++ retrieved = await tracker.get(task.task_id)+ assert retrieved is not None+ assert retrieved.status == TaskStatus.CANCELLED+ assert retrieved.stage == "cancelled"++async def test_get_nonexistent_returns_none(tracker: TaskTracker):assert await tracker.get("does-not-exist") is None