Commit Graph

3150 Commits (22cbcdfa5789b33dbb8d47532c21b24da2fdf5aa)

Author SHA1 Message Date
nullptr-error 22cbcdfa57
feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549)
* feat(sandbox): resolve workspace paths through WorkspaceLayout

Keep remote sessions on the /workspace contract, while file, shell, and
artifact code follow the session layout instead of hardcoded paths.

* fix(sandbox): fail closed on host layout lookup and restore the shell blacklist

Treat host vs remote as an explicit origin, refuse inspect outside ReadRoots,
and skip collection when the output tree is missing or equals the project root.

* fix(sandbox): close leftover fail-open holes in the layout path

Keep host copy, collection, and prompt text from falling back to /workspace when a lookup fails or an adapter returns unclean roots.

* fix(sandbox): satisfy golangci-lint on the layout helpers

Wrap new copy and test lines to 120 columns, and add the exported comments revive requires.

* fix(sandbox): gofmt the read_file layout copy wrap

* feat(sandbox): add host as an OS-enforced backend type

Host is not a named/pinnable config: each command is a fresh local process, unlike Docker, Cube, and E2B.

* feat(session): persist Lite host workspace directory

Empty means an auto-allocated folder. Standard edition leaves the column unused so the shared session model still loads. SQLite also gains the tenant skill tables Lite shell_exec already expects.

* feat(localsandbox): adapt the host OS sandbox to session interfaces

File tools stay outside Seatbelt and are bounded by PathGuard. A revoked project directory fails closed instead of allocating a replacement workspace.

* feat(sandbox): run unpinned Lite sessions on the host backend

Lite opts into host explicitly; web stays disabled when no named remote config exists. Host workspaces are not destroyed or auto-committed.

* feat(desktop): pick and persist host project directories

The native folder dialog is the authorization path; typed paths are rejected. settings.sandbox.host tells the client when this edition can use it.

* fix(sandbox): fail closed on Lite host layout lookup misses

Unpinned host sessions were described as /workspace when pin read or
sandbox resolve failed. Prompts and attachment staging now share the
same fallback so Lite never pretends the remote tree exists.

* fix(frontend): keep settings.sandbox.host in sync with the backend keys

TestDeploymentCapabilityKeysMatchFrontend requires the lists to match. Host stays fail-closed like docker so a missed probe does not show Lite-only UI.

* fix(lint): satisfy golangci-lint on the host sandbox wiring

Check c.Error, wrap lines over 120 columns, put context first on versionsWorkspace, and document exported adapter methods.

---------

Co-authored-by: hailongzhao <hailongzhao@tencent.com>
2026-09-22 19:35:03 +08:00
Ankit Jha 5f1b7a2b3b style: trim the comments added in this branch to the repo's cap 2026-09-22 18:04:32 +08:00
Ankit Jha 0b4cd706c2 fix(knowledge): stop synthesizing never-run stages as failed
Closes #3452.

buildSpanTree fills every canonical stage that has no span row using a
status inferred from knowledge.parse_status, so a failed parse painted
all of them "failed". That status carries information it cannot know:
it cannot tell "this stage ran and failed" from "this stage never ran".

The frontend picks the focused stage with the first failed one in
canonical order, so whenever the real failure left an earlier stage
rowless, the UI named the earlier stage. On main today a text passage is
chunked without ever opening a docreader stage, so an embedding failure
reports 文档解析 / docreader as the broken step for content that has no
document to parse, and four of five segments render as failures.

Once a stage has actually recorded a failure its position is known, so
missing stages are now resolved against it:

- downstream of the failure, per StageDependencies or canonical order →
  cancelled, which is what the status is documented to mean
- before it → skipped; the stage either did not apply or its row was
  never written, and neither of those is a failure

With no failed row anywhere the parse_status fallback stands unchanged,
which is the case it was written for: legacy knowledge parsed before
span tracking existed still renders done/failed rather than five pending
segments.

Also maps parse_status "cancelled" to a cancelled placeholder; it fell
through the switch to pending, so a cancelled document showed spinners
on the stages it never reached.

The DAG walk is redundant with canonical order for today's stage list,
where every dependency points at an earlier stage. It is there so the
answer stays right if the list is reordered or a stage gains an upstream
that is not its predecessor.

No frontend change: skipped and cancelled already render, with localized
labels in every locale.
2026-09-22 18:04:32 +08:00
vicfei 4b0dc3b687 fix: satisfy revive unused-parameter and gofumpt in #3493 review findings 2026-09-22 17:52:49 +08:00
vicfei 2dd36212d6 fix(knowledge): bind extracted images to their document and unscope shared-KB parent lookups
Org-shared KB viewers got 403 for extracted images on current main even
though the retrieving chunk text carried the resource:// handle and the
share grant was valid (#3342, second report after #3022/#3069).

Root cause is a mismatch between ingestion and authorization: document
parses store extracted images through SaveBytes, which registers the
resource but never claims it, while both file proxies (ResolveKBFile and
the message fallback via resourceAccessibleViaSharedKB) authorize through
IsReferencedByKnowledgeBase — which accepts explicit resource_bindings
rows only. The binding-only hardening landed without adding the missing
bindings, so every extracted image became invisible cross-workspace.

Ingestion: bindStoredImages claims each stored image for its knowledge
(ResourceRelationExtractedImage), mirroring bindContentResources — best
effort, same-workspace resources only. Wired into both the file-parse
path (knowledge_process) and manual knowledge (triggerManualProcessing).

Merge: resolveParentChunks fetched parents, grandparents, and image_info
children with the caller's tenant, so shared-KB hits from the sharing
workspace resolved none of them and lost parent context and image_info
(the second defect in the #3342 audit). Those lookups now use the
existing *Only unscoped variants, matching their shared-KB precedent.

Historical documents parsed before this change still carry no bindings;
a backfill is tracked as the follow-up slice.

Verified end-to-end on a fresh stack: publishing a manual document with
an embedded image now records kind=image plus an extracted_image binding
alongside the content-derived attachment binding.
2026-09-22 17:52:49 +08:00
renchao 3a8ce97c29 fix(docker): pull MinIO image from quay.io
MinIO no longer publishes images to Docker Hub: `minio/minio` now returns
"pull access denied", so `docker compose --profile minio|full pull` fails on a
fresh clone. The same tags are still available on MinIO's official Quay
registry, so point the minio and langfuse-minio services there and sync the
dev guide table.
2026-09-22 17:51:12 +08:00
younggglcy 6da4b3acc7 docs: link pgvector issues in retrieval engine guide 2026-09-22 17:49:31 +08:00
Easton Gu d1691cbc26 fix(upload): clean multipart temporary files 2026-09-22 17:40:56 +08:00
wizardchen 733fdd69a6 fix(mcp): proxy server endpoints through frontend gateways 2026-09-22 17:36:03 +08:00
kerthans afeee71e48 fix(wiki): release inflight slot on task cancellation 2026-09-22 17:30:29 +08:00
linus-liu-web 7feac9c570 fix(mcp): resolve MCP endpoint sessions by their storage ID
resolveAskSession stores the session owner as
MCPEndpointPrincipal(...).StorageID() -> "mcp_endpoint:<tenant>:<endpoint>",
but SessionOwnerIDFromContext has no PrincipalMCPEndpoint case and falls
back to the raw user id set by the MCP auth middleware ("mcp-<endpoint>"),
so every ask call failed with
"failed to create user message: session not found".

Handle PrincipalMCPEndpoint the same way as PrincipalEmbedSession.
2026-09-22 16:53:10 +08:00
hailongzhao e55e342387 fix(skill): count create-time sandbox env vars in the required check
A required skill key filled only on the sandbox config was already in the
container, but the gate still treated it as unset.
2026-09-22 16:45:58 +08:00
lyingbug 33c0333ec9
fix(chat): restore shared workspace images after stream completion (#3523)
* fix(chat): restore shared workspace images after stream completion

* test(chat): wrap shared image completion cases for lint

* fix(chat): wait for persisted completion and preserve streamed replies
2026-09-22 00:16:58 +08:00
wizardchen 58260d192c fix(upload): accept XMind files before parser engine discovery 2026-09-21 23:18:11 +08:00
wizardchen da049f04e0 fix(browserskill): release connection capacity when members disconnect
BROWSERSKILL_MAX_CONNECTIONS counted every member a node had served in the
last 90 days: disconnected devices stayed in the device map until their
authorization expired, and devices created by a refused lease claim or a
pre-pairing select had a zero expiry and were never evicted. Once 32 distinct
members had connected, the next one was refused even with one member online.

Capacity now counts live and handshaking members only. Idle devices of other
members are evicted on the next acquisition; interrupted tasks are durable and
reload paused on reconnection. A handshake reserves its device so it cannot be
evicted mid-connect, and a full node reports the capacity error explicitly.

The daemon dial and WebSocket upgrade no longer run under the device lock,
which every other member's device acquisition waits on while holding the
manager lock. The upgrade gains a handshake timeout, and a connection whose
device was revoked, re-paired or evicted while dialing is discarded.
2026-09-21 23:03:11 +08:00
lyingbug 4640a1ea0c
test(rerank): pin every vendor's request, and correct three facts the table surfaced (#3513) 2026-09-21 20:01:09 +08:00
Lvcy_cd cc82e75cb5 fix(parser): keep PDFs on builtin instead of defaulting to anydoc
anydoc has no document model for PDF: Convert force-disables asset
extraction, and AnydocReader falls back to builtin only when the whole
file yields no text. A born-digital PDF with figures and tables that
still yields a few characters is therefore reported as parsed, with its
images, tables and layout silently dropped.

35275d56 widened the type-level preference from ppt/pptx to every type
anydoc claims, and pdf is on that list. The predicate asks whether
anydoc can convert a type, not whether it is the better parser for it.
60207997, one commit earlier, recorded the original intent: "Types
without a fallback (pdf, docx, ...) never consult this".

The builtin PDF parser classifies pages individually and routes scanned
ones through OCR/VLM, so it stays the default for PDF. Every other
anydoc format is unchanged, and an explicit pdf -> anydoc engine rule
still wins.
2026-09-21 16:28:02 +08:00
Leo Dawn 56d075e213
fix(sandbox): resolve a session's pinned sandbox in the workspace that owns it (#3471)
* fix(sandbox): resolve a session's pinned sandbox in the workspace that owns it

tenant_sandbox_configs is keyed by (tenant_id, id), so a config id is not an
address on its own. sessions.sandbox_config_id stored only the id and every
reader took the workspace from the ambient request context.

That holds inside a chat turn, which already runs under the agent's workspace
(WithExecutionTenant in the QA handler). It breaks for a shared agent
everywhere else, because a shared agent runs on ITS OWNER's sandbox config
while the session belongs to the borrower, and these paths run from plain
requests whose only tenant is the session owner's:

  * DeleteSession resolved nothing and returned after a warning. Cube and E2B
    create session sandboxes with onTimeout=pause, so the MicroVM was not
    reclaimed later either: it stayed paused, billing, with nobody holding its
    id. ReapOrphanSandboxes has no production caller, so nothing recovered it.
  * The terminal and desktop panels failed on both branches - attaching to the
    pinned sandbox and provisioning a new one - with ErrSandboxConfigNotFound,
    which terminalErrorFrame maps to a bare INTERNAL close reason. The panel is
    offered unconditionally and already forwards agent_source_tenant_id, so
    this was a dead end the user could reach in one click.
  * Fork read the pin through PinnedSessionSandbox, got no manager, and
    silently degraded to SNAPSHOT_UNSUPPORTED, losing the sandbox state.
  * The fork snapshot lease recorded the session's workspace next to the
    lending workspace's config id, so the reaper could not resolve it either.

Record the owning workspace beside the pin and make the pair travel together
as SandboxPin. Readers now take the workspace from the pin and fall back to the
session's own, which is what every non-borrowed pin (and every row written
before this column) resolves to - so behaviour for own agents is unchanged.

The provision path has no pin yet, so the WebSocket handler passes the agent's
workspace along with its config: terminalProvisionPin already resolved the
shared agent, it just discarded the tenant resolveAgent handed back.

Backfill in the migration is limited to sessions whose pinned config does not
exist in their own workspace - the only ones that can have borrowed it - using
the lending workspace recorded on their newest shared-agent message. Lite has
no shared spaces, so the SQLite mirror adds the column without a backfill.

* fix(sandbox): backfill the pin owner from the config row and self-heal legacy pins

Three gaps in the session sandbox pin owner:

Backfilling from messages.agent_tenant_id names the wrong workspace. The
pin is sticky to the session's FIRST sandbox, while the newest message
carrying agent_tenant_id may belong to a later shared agent from another
workspace. Writing that tenant leaves teardown resolving a config the
workspace does not own - the very failure the column exists to fix.
tenant_sandbox_configs.id is a global UUID primary key, so joining the
config row itself names the workspace that actually owns the pin.

The column was INTEGER while every other tenant id is BIGINT/uint64.
Postgres INTEGER is 32-bit signed, so a large tenant id would not fit.

Rows the SQL backfill cannot reach keep sandbox_config_tenant_id = 0
forever: the reader falls back to the caller's workspace but never
records what it learned. The next chat turn already runs under the
workspace that can resolve the config, so persisting it there is what
lets a later DELETE, panel open or fork find the sandbox. recordOwner
only writes when the lookup actually succeeded and the stored owner is
still unset - stamping the caller's tenant onto a failed lookup would
brand a sticky pin left by a previous shared agent with the wrong
workspace.

Also renames the lentSandboxConfg typo and corrects the fork degrade
reason in a comment: a lent config that resolves to nil fails
BoundSandboxID, so the fork degrades to SANDBOX_GONE, not
SNAPSHOT_UNSUPPORTED.

* fix(sandbox): require a named backend before recording a pin's owner

The self-heal wants to persist an owner "that actually resolved this config",
but a nil error does not prove that. resolveTenantSandboxForConfig returns a
disabled manager with no error twice before it ever reads the config row: for
the workspace kill switch, and for an empty or deployment-default config id.

That reopens the leak this column exists to close. The SQL backfill correctly
leaves sandbox_config_tenant_id at 0 whenever the pinned config belongs to the
session's own workspace, which is most existing rows. One turn of a shared
agent lent by a workspace whose admin has script execution turned off then
short-circuits on the kill switch, returns nil, and stamps that lending
workspace onto a config it does not own. The next DELETE resolves the pin there,
finds nothing, and strands the paused MicroVM.

The pin branch runs before agentConfigID is read, so the shared agent does not
even need a sandbox config of its own to trigger it.

A named backend (Cube/E2B/Docker) is the proof, and it is the same condition
that is already allowed to CREATE a pin further down: what may write a pin may
update whose it is. A config that exists but resolves to type "disabled" is
skipped as well - ownership is genuine there, but such a config never creates a
sandbox, so no teardown can go missing.

The deployment-default pin was inert either way (both workspaces short-circuit
to a manager with no DestroySession), but it is covered too so a later
relaxation of the proof cannot quietly reopen the kill-switch case.

Both tests fail without the guard.

---------

Co-authored-by: hailongzhao <hailongzhao@tencent.com>
2026-09-21 15:38:39 +08:00
lyingbug 41dde3b05f
refactor(models): delete the internal/models/provider façade (#3506) 2026-09-21 14:41:48 +08:00
wizardchen 2d37f8fd7d fix(container): wire shared agent lookup for MCP handler 2026-09-21 14:38:54 +08:00
YG52052051 cab16dcacb
fix(storage): strip storage-backend scope in local provider path normalization (#2979)
KB deletion runs on the process-wide default file service, which is not
wrapped by BackendScopedFileService. Storage-scoped physical paths
("storage://<backend-id>/local://<tenant>/...") therefore reach the raw
local provider, fall through to the relative-path branch, and get joined
under baseDir — producing a nonexistent path so every os.Remove fails with
ENOENT and the physical files silently leak on every KB/document delete.

normalizePathForBase now strips the scope wrapper up front (both the
canonical "storage://" form and the "storage:/" form produced when a path
has been through filepath.Clean), and tolerates the cleaned "local:/"
provider scheme. Unscoped paths are unaffected.

Co-authored-by: YG52052051 <YG52052051@users.noreply.github.com>
2026-09-21 14:21:25 +08:00
林SO f307d3dc94
fix: remove temp file when TempFileContext write fails inside __enter__ (#3495)
* fix: remove temp file when TempFileContext write fails inside __enter__

TempFileContext.__enter__ creates a NamedTemporaryFile(delete=False) and then
calls write()/flush() with no error handling. If either raises (ENOSPC, quota,
any I/O error), the exception escapes __enter__, and Python's with-statement
never calls __exit__ for a context manager whose __enter__ failed. __exit__ is
the only place the file is closed and removed, so the file leaks permanently;
delete=False means GC will not remove it either.

doc_parser.py uses this to spill every uploaded .doc's full bytes to disk, so
one disk-pressure event leaves the whole document behind and makes the
pressure worse.

Wrap write/flush in try/except, close and remove the file, then re-raise.
Success path and the exception-inside-body path are unchanged.

Adds docreader/tests/test_tempfile_context.py covering the failure-injection
case (fails on the old code), the normal roundtrip, and body exceptions.

* fix: unlink temp file even if close() re-raises after ENOSPC

close() flushes again, so the ENOSPC path can skip os.remove and still leak.
Share one cleanup helper that always unlinks and preserves the original error.

---------

Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-21 14:21:07 +08:00
lyingbug 4a9aa06274
refactor(asr): move the wire format into protocol packages, and cover every vendor whose docs offer ASR (#3503) 2026-09-21 13:25:27 +08:00
nullptr-error 7d688cb8d3
feat(localsandbox): add host OS sandbox core and macOS Seatbelt backend (#3472)
* feat(localsandbox): add host OS sandbox core and macOS Seatbelt backend

Introduce a standalone localsandbox package so Lite can run agent
commands on the user's machine under Seatbelt, without linking this
into cmd/server or changing remote sandbox behavior.

* fix(localsandbox): stop post-wait Kill, deny credential files, and refuse swapped roots

Seatbelt was SIGKILL-ing the process group after every successful Wait,
credential write denies omitted literal matches for files, PathGuard
followed a replaced workspace root, and the login-shell test never ran -lc.

* fix(localsandbox): tighten app-data deny, env inherit, and post-wait Kill

Keep ask/full from silently widening to auto, stop inheriting host secrets,
and make session workspaces survive midnight so the Seatbelt kernel stays
fail-closed before adapter wiring.

* fix(localsandbox): pass gofmt, errcheck, and revive on the new package

CI now lints from the merge base, so the host sandbox needs formatted tests, checked Close() results, and exported comments.

* fix(localsandbox): refuse broad workspaces and stop leaking host secrets

Keep the host sandbox fail-closed before adapter wiring: home/Users/Volumes
cannot be the workspace, login shells no longer re-import rc secrets, and
Service applies the env allowlist on every Run. Also close the Lstat TOCTOU
and post-wait Kill window.

* fix(localsandbox): check Close() in seatbelt spawn tests

* fix(localsandbox): deny host temp dirs and refuse leftover wide workspaces

file-read* still left /tmp and /var/folders readable, and Build/Relax
accepted /private/var, /System/Volumes/Data, and ~/Library/** as a
workspace, which re-opened the private deny. Also re-open only ~/.local/bin.

---------

Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-21 13:24:05 +08:00
younggglcy 2c5f90b78e chore: remove empty root package-lock.json 2026-09-21 13:16:19 +08:00
tayadwx 911a1490ea
fix(wiki): enrich cited chunk content with image captions in Reduce phase 实体/摘要页面引用子块时同样使用富文本内容 (#3292)
* fix(wiki): enrich cited chunk content with image captions in Reduce phase

The citation-pass comment claims image/ocr chunks are "already merged
into the text content via reconstructEnrichedContent", but that function
has exactly one call site — the per-document summary page. The Reduce /
page-edit phase resolves cited chunks straight from the repository and
feeds the LLM bare Markdown image links with no caption or OCR context.

This mismatch is observable: on concept/entity pages generated from an
image-heavy knowledge base, the page-editing model receives
![filename](wkimg:NNN) with no way to judge the image, and empirically
either copies the image links verbatim or drops them wholesale —
including real content images. A controlled experiment where the same
image reached two pages byte-identically produced opposite keep/drop
decisions, and <image_caption> appeared in 0 of 9 outgoing page-edit
requests.

resolveCitedChunks now collects image_info for the cited text chunks
(CollectImageInfoByChunkIDs, one query per knowledge batch, outside the
per-page loop) and inlines it with EnrichContentWithImageInfo — the same
enrichment the summary path uses. Image info collection skips disabled
child chunks, so downstream decorative-image cleanup composes with this
path automatically.

* test(wiki): cover cited-chunk image enrichment in resolveCitedChunks

Add unit tests asserting that cited text chunks reach the Reduce phase
enriched with caption/OCR info from their enabled image child chunks,
that disabled children are skipped (decorative-image cleanup composes),
and that unknown chunk IDs still fall through gracefully.

---------

Co-authored-by: tayadwx <tayadwx@users.noreply.github.com>
2026-09-21 13:13:48 +08:00
ChenRussell 5036062355 fix(wiki): degrade gracefully when revision snapshot is missing
The page revision history feature (introduced in 0.7.2) only snapshots
a version when an edit supersedes it, so versions written before the
upgrade (e.g. upgrading from 0.7.1 straight to 0.8.0) and old snapshots
removed by retention return 404 when fetched by version. The revision
drawer treated this as an error and surfaced a "wiki page revision not
found" toast.

A missing snapshot is a legal state, not a failure: loadVersionSnapshot
returns null on 404 (cached), the diff pane falls back to showing the
version's full content when the baseline snapshot is missing, a hint
state replaces the diff when the selected version's own snapshot is
gone, and the toast uses localized messages.
2026-09-21 13:10:52 +08:00
LuckVd 07e6e2984a
fix(knowledge): hide mutation controls on read-only shared knowledge bases (#3103)
A user browsing an org-shared knowledge base with a viewer (read-only)
grant still saw upload/edit/delete/settings controls when they are an
admin of their own personal workspace: KnowledgeBase.vue's canEdit /
canManage fell back to authStore.hasRole('admin') (the LOCAL tenant
role) unless the org share list happened to contain the KB, and
KnowledgeBaseList.vue's canManageKBCard never consulted the share
permission at all. Every such click is rejected by the backend's
3-D permission cap (effective = min(share, org_role, tenant_role),
RequireKBAccess requires OrgRoleEditor for mutations), so the buttons
were dead ends that made read-only shares look writable (#3098).

Both surfaces now treat an explicit cross-tenant grant as the single
source of truth: the effective permission (org share list entry or
my_permission from GET /knowledge-bases/:id, already capped
server-side) is checked first, and only owner/admin/editor grants
enable editing (owner/admin for manage). The local admin/creator
fallbacks stay reserved for own-tenant knowledge bases, so no
previously working action is hidden. Shared cards that lost their
permission field through the pin/recents merges (isMine === false)
are treated as read-only instead of falling through to the local
admin role.

Fixes #3098

Co-authored-by: LuckVd <LuckVd@users.noreply.github.com>
2026-09-21 13:10:20 +08:00
JYao-Chen 07a5af673e
fix(graph): increase entity extraction output budget (#2859) 2026-09-21 13:08:49 +08:00
JYao-Chen e3d52e8353
fix(wiki): skip ambiguous single-Han auto-links (#2860) 2026-09-21 13:08:04 +08:00
HJX|翻斗花园二蛋 2f2bcc7aa7
fix(suggestion): avoid echoing the current query (#2582)
* fix(suggestion): avoid echoing the current query

* test(suggestion): use generic follow-up examples
2026-09-21 13:06:01 +08:00
Leo Dawn a3c7be10ca
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run

Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:

  * @Skill was always empty, in every skills mode. GET /skills resolves the
    sandbox config with (tenant, id), and the agent's config id does not exist
    in the borrower's workspace - effectiveTenantSkills short-circuits before
    it reads a single skill row. The response still reported
    skills_available: true, so the category appeared with nothing under it.
  * @MCP in "selected" mode was empty for the same reason: the preset holds the
    owner's service ids, which match nothing in the borrower's catalog.
  * @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
    for every enabled service, so the picker offered the BORROWER's own
    services; resolvePerRequestMCPScope then intersected the mention against
    the agent's preset, found nothing, and dropped it with only a warning.

Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.

Both endpoints serve only what the agent can actually invoke:

  * skills: the sandbox config comes from the agent, never from the query, so a
    borrower cannot name another config in the owner's workspace and enumerate
    it; "selected" narrows to the configured names and a disabled picker never
    reaches the owner's inventory at all.
  * MCP: the agent's explicit preset and nothing else, because that is exactly
    what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
    offers nothing rather than offering services every mention would drop.
    Whether "all" should become mentionable is a separate decision about that
    guard, not about the picker.

The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.

On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.

* fix(agent): check the c.Error return on the shared-agent picker paths

golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 13:02:34 +08:00
Fuzzycorrect 13d0b9f94a
fix(queue): order ClaimBatch by fail_count so retries cannot starve new work (#3444)
* fix(queue): order ClaimBatch by fail_count so retries cannot starve new work

ClaimBatch picks work with ORDER BY id ASC (Postgres) / MIN(id) ASC (GORM),
i.e. oldest-first by primary key.

Retry does not move a row: requeueFailedOps calls IncrFailCount and then
ReleaseByIDs, which deliberately keeps the row in place so the fail_count
budget keeps counting down.

Together those two produce a starvation mode. A document that fails
repeatedly keeps its original, lowest id, so it is always selected first,
while every never-attempted document - enqueued later, hence a higher id -
waits behind it until the stuck op finally exhausts wikiMaxFailRetries and
dead-letters. Observed on a real 87-document KB: 42 ops pending, the four
head ops were all re-runs of documents that already had a root page, and 38
never-attempted documents were never claimed. With 8 concurrent workers and
a drained asynq queue (pending=0, retry=0, active=4), the bottleneck was the
claim ordering, not throughput.

Order by least-failed first, oldest-first within the same fail_count. When
every row has fail_count = 0 this degenerates exactly to the previous
id-ordering, so the existing FIFO tests still pass.

Test: TestTaskPendingOps_ClaimBatch_PrefersLeastFailed replays the real retry
path (claim -> IncrFailCount -> ReleaseByIDs) and asserts that the
never-attempted document is claimed first and the retried one is still
eventually picked up rather than stranded.

* fix(queue): apply the same least-failed PeekBatch order in Lite mode

Lite wiki ingest still peeks rather than claims. Ordering that path by
id alone would keep the same head-of-line starvation this PR already
fixed for ClaimBatch. Align comments with the new selection rule.

---------

Co-authored-by: wizardchen <wizardchen@tencent.com>
Co-authored-by: lyingbug <xiayula2014@gmail.com>
2026-09-21 13:00:29 +08:00
lyingbug 9030f556f2
refactor(embedding): move the wire formats into protocol packages, and correct the vendor facts (#3500) 2026-09-21 11:55:38 +08:00
lyingbug d6c7665cb5
refactor(rerank): move the wire formats into protocol packages, and correct the vendor facts (#3488)
把 8 个手写 rerank client 收敛成 3 个协议包(internal/models/api/{cohererank,dashscoperank,nimrerank}),每个带一份从厂商文档逐字粘来的 golden fixture。LKEAP 和火山保留官方 SDK,但只实现 api.Reranker。rerank.NewReranker 改成 catalog.Resolve + 按协议分发,函数体里没有厂商名。生产代码净 -147 行。

三件原来每个 client 各管各、现在由共用层统一的事:分批(按厂商声明的上限,智谱 128 / NIM 512 等此前无人执行)、分数标度(NIM 与 GPUStack 返回 logit,其余为 0~1 概率,统一换算后再与 RerankThreshold 比较)、NIM 的 truncate(默认 NONE 会让超长 passage 整请求失败,此前从未发送)。

对照官方文档订正四处厂商事实:GPUStack 分数标度、千帆三项上限、阿里 500 条上限、qwen3-rerank 是第二种协议(隐藏并在构造期拒绝)。Novita / OpenRouter / LiteLLM 三家官方有 rerank 但未开类型,本次开放,零协议代码。

运行时行为变更:NVIDIA 起发 truncate: END;GPUStack 分数按 logit 换算(可按行覆盖);truncate_prompt_tokens 收窄为厂商声明制;qwen3-rerank 存量行从静默打错变为构造期报错。详见 PR 描述。

关联 #3486。
2026-09-21 10:36:26 +08:00
lyingbug 9aa63c866f
fix(models): correct four vendor request shapes that contradict the docs (#3489)
PR #3470 逐条核对了 27 家厂商,但有四处(五个 entry)的声明与厂商文档对不上,
其中一处在默认路径上就是错的。

1. DeepSeek:参考文档写明 "required and named tool choices are not supported
   in thinking mode; the API returns a 400 error",而 vendor.go 的事实注释写成
   「plain OpenAI set」并因此没有配 tool_choice_modes,默认继承全集。现在只发
   none / auto,和 Moonshot 同类限制的写法一致。compat 无法表达「仅思考模式下
   受限」,而思考是 DeepSeek 的默认状态,所以不做条件区分。

2. 智谱 glm-5.2:官方对 GLM-5.2 的折叠规则是 none|minimal 放弃思考、low|medium
   折成 high。models.json 却把 minimal 改写成 "low",发出去被平台折成 high,
   用户选最弱档实际跑最高强度。删掉该覆盖,回落到厂商级的 minimal。

3-4. NVIDIA NIM:z-ai/glm-5.3 与 moonshotai/kimi-k3 配了 thinking_levels,却
   沿用厂商默认的 chat-template-kwargs 编码——该分支只发 enable_thinking 布尔,
   档位被整个丢弃,而前端仍按 capabilities 渲染出一个不起作用的档位选择器。
   两个 entry 改为顶层 reasoning_effort。kimi-k3 的 NIM 卡片只写了
   "configurable low, high, or max reasoning effort" 而未展示参数,按其第一方
   API 的字段推定,已记入 Unverified。

5. 阿里云 qwen3.8:官方参数表写明「不支持 reasoning_effort 与 thinking_budget
   同时设置,同时设置会报错」,vendor.go 的注释也记了这条,但 applyThinking 在
   ThinkingBudgetTokens > 0 时无条件两个都写。新增 compat 键
   thinking_budget_excludes_effort,冲突时档位优先、预算让路。

3 与 4 在当前默认路径上已经影响到档位;1 和 5 目前够不到(全仓库只有
agent/finalize.go 发 tool_choice="none",且没有任何地方给 ThinkingBudgetTokens
赋值),属于修掉的埋雷。2 是默认路径上的实际错误。

新断言做过反向验证:数据回滚到修改前,7 条 vendor 断言与新增的 golden 用例全部
报错。
2026-09-21 00:05:11 +08:00
lyingbug c40a9dd194
refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470)
把集中式的 OpenAI 方言客户端重建为三层:协议层 internal/models/api/<protocol>(OpenAI Chat Completions / OpenAI Responses / Anthropic Messages / Google GenAI),目录层 internal/models/catalog,厂商层 internal/models/vendors/<id>(27 家,go:embed)。27 家厂商逐条对照官方文档核验,厂商事实以数据声明,协议层不出现厂商名。

既有行的运行时行为会变的四处:
1. extra_config.api 变为保留的协议选择器,非法取值被拒绝;
2. Azure 未填 api_version 的行改走 /openai/v1 GA 数据面;
3. api.openai.com 的一方流量改走 Responses 协议(中转/网关不受影响);
4. 7 家厂商的输出上限字段按文档纠正。

不需要数据库迁移。详见 PR 描述与 website-docs/03-features/06-models.md。
2026-09-20 21:28:23 +08:00
lyingbug 50bbf9ad5f
fix(embedding): two wire-level bugs in the remote embedders (#3484)
* fix(embedding): stop four embedders from returning (nil, nil) on a dead upstream

doRequestWithRetry keeps resp and err in the enclosing scope and returns
(nil, err) once the retries run out. Writing `req, err := ...` inside the
loop introduces a loop-local err, so `resp, err = client.Do(req)` updates
that copy and the enclosing err stays nil. Every retry failing therefore
returned (nil, nil): BatchEmbed's `if err != nil` did not fire and the next
line dereferenced resp.Body, a nil-pointer panic that took the process down
instead of surfacing "connection refused". Stopping the embedding upstream
and asking any RAG question reproduced it.

The bug was found, documented and fixed in openai.go, then re-fixed in
zhipu.go and again, differently, in azure_openai.go; jina.go, nvidia.go,
aliyun.go and volcengine.go kept the original spelling because the retry
loop exists in eight near-identical copies and a fix to one reaches none of
the others.

TestEmbeddersReportUnreachableUpstream now drives all eight against a
refused port with the retry budget zeroed, so the next copy of this loop is
covered the moment it appears.

* fix(embedding): read the documented index field of DashScope multimodal vectors

The multimodal embedding response carries each vector's position in `index`:

  {"output": {"embeddings": [{"index": 0, "embedding": [...], "type": "text"},
                             {"index": 1, "embedding": [...], "type": "image"}]}}

The decoder read `text_index`, which is the field name of DashScope's *text*
embedding API one page over. A missing field decodes as 0, so every vector in
a batch was written to slot 0 and the rest of the slice stayed empty: with the
default BATCH_EMBED_SIZE of 5, four of every five chunks were indexed with an
empty vector. Only models routed to this client are affected — an Aliyun
embedding model whose name contains "vision" or "multimodal"; the text-only
models go through the OpenAI-compatible path.

An index outside the batch, or a response that covers fewer inputs than were
sent, is now an error. Handing back an empty vector stores it and poisons
retrieval without anything failing.

The regression test replays the response example from the reference page, so
the decoder is pinned against the vendor's own document rather than against
our structs.

https://help.aliyun.com/zh/model-studio/multimodal-embedding-api-reference
2026-09-20 21:12:33 +08:00
nullptr-error d4b9fd0c72
feat(session): in-place rewind with workspace checkpoint reset (#3469)
* refactor(session): extract shared workspace git reset helper

Fork bootstrap and in-place rewind both need the same /workspace reset
script. Move it out of fork_bootstrapper so rewind can reuse it without
copying ExecShellCommand plumbing.

* feat(message): add DeleteMessagesFrom for rewind truncation

Rewind needs a composite created_at+id cursor so same-millisecond
messages after the fork point are not left behind. Delete inside a
transaction and return the rows so callers can log the count.

* feat(session): add in-place rewind service

Truncate the current session at a user or assistant message and git-reset
the live sandbox when a reachable checkpoint exists. Reset failure is
fail-closed so messages are never deleted ahead of the workspace.

* feat(session): expose POST /sessions/:id/rewind

Wire SessionRewindService through the session handler and container so
the client can rewind in place. Busy sources map to 409; git reset
failures stay 500.

* feat(chat): add rewind API client and i18n copy

Expose rewindSession with the same 180s timeout as fork, and add
chat.rewind strings in all five locales so the confirm dialog is not
hardcoded Chinese.

* feat(chat): add rewind button with confirm dialog

Offer in-place rewind next to fork on user and assistant messages.
Confirm before truncating the thread, then reset locally and prefill
the original question when the cut point is a user message.

* fix(chat): confirm rewind with a compact popover

Replace the full-page confirm dialog with a small popconfirm above the
rewind button, and shorten the swagger description to satisfy line length.

* fix(chat): skip markdown images with an empty destination

Empty ![]() was rendered as <img src="">, which shows as a broken image in the reply.

* fix(chat): clip sandbox panel slide-in and rename open tooltip

Keep the enter animation inside the viewport so it cannot flash a document
scrollbar and jump the page. Rename the header toggle from sandbox terminal
to expand sidebar.

* fix(session): 回滚时完成截断、互斥发送并重拉历史

客户端中断不再让 git reset 成功后留下未删的消息;reset/删消息前复查 busy,并锁住当前会话输入。成功后重拉历史,避免切会话预填串台和 steer 分段截错。

* fix(session): 工作区 git 外置,并让未开箱 fork 的回滚对齐 SHA

checkpoint、回滚和 fork 共用 --git-dir=/var/lib/weknora/workspace.git,避免 rm -rf /workspace 丢掉历史。
未开箱分叉会话回滚时改写或清除 ForkBootstrap,避免第一次开箱仍 reset 到旧切点。

* fix(session): 回滚加会话锁,并堵住发送中回滚与脏 SHA

同一会话并发回滚互斥(进程内 + Redis),冲突返回 409。发送/流式中禁用回滚按钮;历史重拉成功后再改界面。未开箱 fork 的 checkpoint SHA 必须是 40 位 hex。工作区 git 只认外置 GIT_DIR,不再 mv/rm 根上的 .git。

* fix(session): 未开箱 fork 回滚成功时不要提示没有沙箱

ForkBootstrap 已对齐切点后返回 workspace_reset,前端走「已回滚」;清掉引导时改报 NO_CHECKPOINT。纯聊天仍用 NO_SANDBOX。

* fix(chat): stub composerLocked in steer interaction tests

handleSteerMsg now bails when rewind locks the composer. The VM harness
did not provide that ref, so CI threw ReferenceError on every steer case.

* fix(session): allow git-layout mkdir in pinned checkpoint test

SkipWorkspacePrep still omits the input/output bootstrap exec. The
checkpoint script now mkdirs WORK_TREE and GIT_DIR, so the old
"command must not contain mkdir" assertion was stale.

* fix(session): block send while rewind holds the session lock

BeginTurn now fails if the rewind key is held, so AgentQA cannot start
during git reset. Knowledge-chat and executeQA check the same lock
before persisting messages. composerLocked is still page-local.

* fix(session): close send/rewind races and fail closed without a checkpoint

Hold the turn lease before persist so rewind cannot truncate an in-flight
send. TryLockRewind fails while a turn is live; emptying the conversation
resets the workspace through the same git path as a SHA rewind.

* fix(session): wrap rewind reset call to satisfy line length lint

* fix(session): fail closed on a replaced sandbox and close leftover rewind holes

Do not truncate history when the live workspace cannot follow the checkpoint, retry deletes after a successful git reset, and keep send and rewind exclusive even without a sandbox manager.

* fix(session): address rewind review findings

Correctness:

- Adopt a pre-move /workspace/.git into SessionGitDir on first use. Sandboxes
  provisioned before the git dir moved out of the work tree still hold their
  checkpoints there, so every SHA recorded on those turns stopped resolving:
  rewind 500'd instead of truncating and fork's rollback failed on a bad
  object. Adoption is guarded on the new dir being absent and on the work-tree
  repo carrying the checkpointer's committer identity, so an agent's own
  `git init` in /workspace is left alone.
- Only fail a send closed when BeginSessionTurn reports a rewind lock. Every
  other lease error degrades to running without a lease, as before rewind
  existed; a Redis blip on the lease script was rejecting the user's message.
- Release an abandoned fork snapshot after the truncate lands, not before.
  Clearing the bootstrap column is a DB write a retry can redo; deleting the
  snapshot is not, and a delete that exhausted its retries left the fork with
  no workspace to boot from under a full transcript.
- Re-acquire the rewind lock when a renew call errors, not just when it
  reports the lease lost. Sitting the tick out let a short Redis outage expire
  the 2m key mid-reset and hand the session to a second rewind.
- Keep the history cursor when a post-rewind reload fails. Clearing it sent
  the next scroll-up back to the newest page, which the kept prefix already
  holds, instead of to older messages.

Efficiency:

- Add ListAssistantCheckpointsUpTo and use it to answer "which checkpoint does
  kept history still reach". Rewind was reading every message body and artifact
  row in the session to find one SHA.
- Take the send-side turn lease once per request. The handler already holds it
  before persisting; QARequest.TurnLeaseHeld lets the QA services skip their
  own, so IM/MCP still take one and HTTP send stops doubling every rewind check
  and lease round trip.
- Batch the suggestion delete behind DeleteByMessageIDs instead of one query
  per deleted message while the rewind lock is held.
- Resolve the rewind affordance for the whole transcript in one pass rather
  than two list scans per rendered row on every streamed token.

Cleanup:

- Drop the SANDBOX_REPLACED skip reason, which the service never produces (it
  fail-closes with a 409), along with its copy in all five locales.
- Drop GetMessagesBySession from rewindMessageStore; rewind never calls it.
- Drop the unused chat.rewind.confirmTitle copy from all five locales.
- Restore the chatHeader.toggleSandboxPanel wording this branch had changed
  unrelated to rewind.

---------

Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-20 21:01:00 +08:00
wizardchen 9b0b0910ba ci(lint): filter new issues from git instead of the PR patch API
`only-new-issues: true` makes golangci-lint-action ask the GitHub API for
the pull request patch, and that endpoint refuses any diff over 20000
lines:

    failed to fetch pull request patch: RequestError [HttpError]:
    Sorry, the diff exceeded the maximum number of lines (20000):
    {"resource":"PullRequest","field":"diff","code":"too_large"}

The action does not fail on that error — it falls back to linting
everything. With the backlog this repository carries (5672 findings at
the time of writing) the job then reports hundreds of issues in files the
pull request never touched, capped at 50 per linter, and cannot pass no
matter how clean the change is. Tencent/WeKnora#3470 hit this at 28050
diff lines.

golangci-lint computes the same filter itself with --new-from-merge-base,
which reads git rather than the API and has no size limit. `fetch-depth: 0`
on the checkout is what makes the base commit available, and the changes
job above already relies on it, so nothing else needs to move.

Verified against that pull request's branch: full run reports 5672 issues,
`--new-from-merge-base=<base sha>` reports 0, both from the repository root
and from the cli module directory.
2026-09-20 17:42:43 +08:00
lyingbug 7fa6f028fc
feat(chat): rework the saved-answer Markdown editor (#3467)
* feat(chat): make the saved-answer Markdown editor usable

Saving a chat answer to a knowledge base dropped the raw answer — citation
tags and all — into a 760px drawer with a single column and a preview
toggle. Every `<kb doc=… chunk_id=… kb_id=…/>` had to be deleted by hand,
and the title arrived hard-cut with an ellipsis.

- strip `<kb/>` tags from the seeded draft and list the cited documents
  once at the bottom; keep `<web/>` citations as real Markdown links
- cut a long question on a clause boundary instead of at 40 chars + "..."
- compact title + knowledge-base row, full-height editor (no more
  calc(100vh - 360px)), and an edit / split / preview switch that starts
  on split whenever the drawer is wide enough for two panes
- opt-in maximize toggle on SettingDrawer for full-width editing
- native textarea in place of t-textarea, with Cmd/Ctrl+B/I/K/S/Enter,
  list continuation on Enter and Tab indenting
- confirm before discarding unsaved changes; show a character/line count

* style(chat): stop painting the Markdown editor green

Brand green was doing decorative work in the editor: a 4px green rule and
an 8% green wash on every blockquote in the preview, a green pill for the
active view, green toolbar hovers, and a green halo around the whole
editor while it had focus. Together they read as noise.

Green now means action only — the publish button, links, keyboard focus
rings. Blockquotes match the chat renderer's own neutral treatment
(2px --td-component-stroke, no fill), the view switch uses a raised
neutral pill, and toolbar hover/active use the container tokens.

* refactor(chat): fold the editor toolbar from 16 buttons to 10

Sixteen same-sized icons gave equal weight to "bold" and "insert
horizontal rule", and at the default drawer width the row already ran out
of space (the overflow scrollbar is hidden, so the last icons simply
vanished).

Headings collapse into one labelled "标题" menu and the block inserts
(quote, code block, table, image, rule) into "插入"; the actions people
reach for mid-sentence — bold/italic/strike/inline code, the three list
kinds, link — stay one click away. The cropped shortcut line in the
status bar becomes a keyboard button whose tooltip lists every binding,
including Cmd+Enter to publish, which was undocumented.

* refactor(chat): collapse the editor into four bands

The drawer stacked seven horizontal strips — header, subtitle, a labelled
title row, a labelled knowledge-base row, toolbar, canvas, status bar —
each with its own rule or tint, and none of them shared a left edge.

The document now has a head (a borderless title that reads as a title,
with "存入 <知识库>" as a sentence under it), one editor card, and a
footer. The header subtitle, the two field labels and the status strip
are gone: word/line counts moved next to the draft tag in the footer and
the shortcut sheet to the toolbar's right end. Title, destination and the
editor card all start on the same left edge.

* refactor(chat): move the destination onto the title row

"存入" was a verb invented for this one screen, and the row it sat on cost
the head a whole line. The destination is now the folder icon this product
already uses for a knowledge base in the @-mention list, followed by the
select, parked at the right end of the title row — so the head is one line
whose two ends line up with the editor card below it.

The narrow layout that stacks them again is now driven by the editor's
ResizeObserver instead of a `@container` query: the scoped-style pipeline
drops `@container` blocks (none of them reach the page), so the previous
rules — stacking and, worse, the toolbar wrap that keeps the last buttons
from silently overflowing — never actually applied.

* style(chat): let the destination select reach the right edge

The select's box sat flush with the editor card, but a 132px min-width
held it open past its content, so the folder icon, the name and the caret
stopped 43px short — the block read as floating rather than aligned. It
now shrinks to its content (up to 260px, then truncates), leaving only the
control's own 5px of inner padding, and stays right-aligned when the
narrow layout stacks it under the title.

* style(chat): keep the narrow toolbar on one row

A narrow drawer wrapped the toolbar into two rows and left the view switch
hanging beside the second one, and the head split into two rows as well.
The view switch now drops its three text labels on narrow (the tooltips
already carry them), which buys back enough room for the whole toolbar —
all ten controls — to stay on one row down to the 600px minimum width.
The head keeps title and destination on one line too, now that the
destination shrank to its content.

* style(chat): shrink the editor drawer's header

The header was sized for a title plus a subtitle; with the subtitle gone
it kept 65px for one 15px line — a 32px badge inside 14px of padding,
propped up by TDesign's 56px min-height. Down to 47px: 26px badge, 9px
padding, min-height released. Scoped to this drawer, so the other
SettingDrawer consumers keep their proportions.

* chore(chat): drop leftovers from the editor rework

- the per-button `btn-<key>` class on toolbar buttons: nothing styled or
  queried it
- `INDENT` was exported but only ever used inside markdownEditing
- a comment still pointed at the status bar that no longer exists
2026-09-20 16:16:28 +08:00
wizardchen a62a51e6b0 docs(db): stop claiming MySQL is a supported DB_DRIVER
initDatabase() only switches on postgres and sqlite; any other DB_DRIVER
returns "unsupported database driver". The .env.example comment listed
mysql as a valid value, so anyone following it hit a startup failure.

migrations/mysql/00-init-db.sql is the other source of that impression --
it is kept roughly in sync whenever the base tables change, but nothing
references it. Add a README stating it is unwired and listing what real
MySQL support would still need.

Also correct the table count in the database-schema doc: that script now
covers 10 tables, not 7.
2026-09-20 13:29:03 +08:00
lyingbug 093c16cbb3
fix(agent): end the turn when the completion cap truncates an answer (#3449)
* fix(agent): end the turn when the completion cap truncates an answer

A round that came back with finish_reason=length and no tool calls used to
fall through analyzeResponse as non-terminal. With no tool calls to run,
appendToolResults pushed the half-written answer back into the history as a
plain assistant message with nothing telling the model to continue, so the
next round rewrote the answer from the top, hit the same cap, and repeated
until the round budget ran out or the user cancelled.

Three guards, each independently load-bearing:

- analyzeResponse gains a truncation case: deliver the partial text and end
  the turn. A cap hit that produced no text at all has nothing to hand over
  and defers to the existing bounded empty-content retry. No continuation
  nudge is sent — it only grows the prompt with the discarded fragment, and
  assistant prefill, the reliable form, is unavailable on most
  OpenAI-compatible endpoints.
- A consecutive-length breaker stops the loop when truncation keeps landing
  inside tool-call arguments, the one shape that still legitimately loops.
- The stuck-loop detector now counts an identical lack of content. A model
  burning its whole budget on reasoning returns empty every round, which used
  to land in the else branch and reset the counter, so the guard could never
  fire on the case that needed it most.

The answer event and its Done marker carry a truncated flag through the SSE
metadata, and the chat UI shows the same style of notice it already uses for
fallback answers.

Fixes #3446

* fix(agent): treat unterminated reasoning as no answer and keep the truncation marker

Review follow-up on three ways the truncation path could still mislead.

An inline <think> block that the cap cut off never gets its closing tag, so
the pair regex left it whole and the round looked like it had produced an
answer. The truncation case then skipped the empty-content retry it exists
for and emitted the chain of thought as the final answer. StripThinkBlocks
now drops a dangling <think> through end of input, which is what the
streaming splitter already does with the same text, so the two agree on what
counts as an answer.

The truncation marker only rode on the SSE metadata, and a finished
conversation is rebuilt from agent_steps rather than replayed from stream
events. A reloaded, shared or re-opened turn therefore lost the notice and
its half-written answer looked complete. AgentStep carries the flag now — it
is a jsonb column, so no migration — and the frontend marks the rebuilt
answer event and message from it.

The consecutive-length guard ran before the round was recorded, so a final
round of "let me look that up" plus a truncated tool call handed that
preamble over as the final answer, with its tool calls never refused and the
preamble never retracted from the answer area. The guard now records the
round and fails its tool calls the way every other truncated round does, and
a response carrying tool calls never donates its text as the answer.
2026-09-20 13:17:50 +08:00
lyingbug eaca7364cd
fix(knowledge): stop two parse paths from stranding a row in processing (#3445)
* fix(knowledge): stop two parse paths from stranding a row in processing

Both of these left parse_status on "processing" with nothing left to move
it, so the document sat there until the housekeeping sweep failed it an
hour later with "task stuck in processing, recovered by housekeeping" —
reaching the user as a slow, unexplained parse failure.

processChunks returned silently when the embedding model could not be
resolved (deleted model, expired credentials, unreachable provider). It
reports nothing to its callers, so the attempt ended with the row still
marked processing. Persist the failure with the underlying cause and
record the embedding stage as failed, matching how the sibling
GetASRModel branch already handles a model it cannot resolve.

enqueueImageMultimodalTasks seeded the multimodal fan-in counter with
len(images) before the fan-out, then only warned when a payload failed
to marshal or asynq refused the write. Those slots have no task behind
them to drain them, so the counter never reached zero and post-process
was never triggered. Release the shortfall after the fan-out, and drive
post-process directly when the release itself brings the counter to zero
or when nothing reached the queue at all — the same reconciliation
KnowledgePostProcess already performs for its enrichment subtasks.

Seeding the counter before enqueueing is kept deliberately: an image
task that finishes early would otherwise decrement a key the fan-out is
about to overwrite. The release is what settles the resulting gap.

Also collapse the Redis key into multimodalPendingKey so the fan-out and
fan-in cannot drift, and share one enqueueKnowledgePostProcessTask
between the no-multimodal path and the slot release.

* fix(knowledge): keep the embedding-model guard from clobbering abort state

Review follow-up on the two stranded-row fixes.

The embedding-model guard wrote parse_status=failed unconditionally, which
made two cases worse than the silent return it replaced. Resolving a model
reads the database, so the entry guard's verdict is already stale by the
time the error comes back:

  - A user cancel signals asynq CancelProcessing, which cancels the
    handler context. GetEmbeddingModel then fails with context.Canceled
    and the guard overwrote "cancelled" with "failed: ... context
    canceled" — mislabelling an interrupted run as a broken model. Worker
    preemption and graceful shutdown look the same.
  - ReplaceKnowledgeFile keeps the knowledge ID and swaps file_path.
    UpdateKnowledge is a full-row Save and file_path is not among
    omitFieldsOnUpdate, so the stale row rolled the path back over the
    replacement and marked the replacement's live attempt failed.

So the guard now returns without writing when the context is gone or the
error is a context error, re-checks isKnowledgeAborted before writing, and
persists through updateKnowledgeUnlessSourceReplaced like the completion
path already does.

The multimodal slot release gave up on the first Redis error. Since the
release is the only thing that can drain a slot with no task behind it,
that put the row straight back into the stranded state this fix exists to
remove. It now retries a bounded number of times and logs at error level
when it still cannot settle the counter. DECRBY is not idempotent, so a
retry after a lost reply can over-decrement; that finalizes early rather
than stranding, which is the direction to fail in, and post-process
already tolerates repeat deliveries. The nothing-enqueued branch's Del
also moves onto the detached context, so a cancelled parent no longer
leaves the seeded key to expire on its own 24h TTL.

Two reporting corrections while here: the span now carries the same
message as the document list instead of a fixed generic string, because
the timeline renders error_message and withholds error_detail; and the
comment no longer claims expired credentials or an unreachable provider
surface here — resolving a model only reads the row and constructs a
client, so those fail later on the first BatchIndex round-trip.

Adds 6 tests for the paths above. Two of them initially passed against
the unfixed code because the entry guard caught them before the model
call, so the repository fake now serves the cancelled / replaced row only
after the guard's own reads, which is the window that actually exists.
2026-09-20 13:02:05 +08:00
lyingbug 9e0c28fbc9
feat(artifacts): let users delete generated files (#3447)
* feat(artifacts): let users delete generated files

技能生成的文件此前只能列出、预览和下载,没有任何删除入口,后端
handler/service/repository 三层都没有实现。DeleteSession 里那句注释
("a dedicated GC job or explicit hard-delete API is the right place to
reclaim storage") 留的就是这个位置。

Tombstone, not a row delete. message_artifacts 新增 deleted_at,删除只
打标记不删行。position 是下载接口寻址用的下标(msg.Artifacts[index]),
物理删掉中间一行会让它后面的文件整体前移,已有链接下到错的文件;而
ArtifactCollector 的去重集就是从这张表读的,行没了之后沙箱里 mtime 未变
的同名文件下一轮就会被重新采集回来,删了又出现。墓碑同时挡住这两条。
loadArtifactsByMessage 因此刻意保留墓碑,展示侧用 MessageArtifacts.Live
过滤;NewMessageArtifactRecords 带上 DeletedAt,任何一次消息重写都能把
墓碑原样写回去。

Bytes. 字节是真回收的,走 catalog.Release 引用计数:一份文件可能同时被
知识库条目和后续回答持有,只有所有持有者都放手才 DeleteFile。删多个版本
时同一个对象可能来自多条消息,每条消息的绑定都要释放,字节只删一次。
回收发生在打标记之后,且只针对本次调用真正认领到的行——并发的第二次删除
认领不到任何行,也就不会去动别人正在回收的对象。释放失败一律保留字节:
留个孤儿 blob 以后还能靠墓碑上的 url 回收,删错了则是别人的文档里的图片
凭空消失。

Scope. 会话内面板每个版本单独一行,删的就是点的那一版;产物库一行代表
一个文件(带版本数),默认连同该文件在这个会话里的所有版本一起删,否则
删完这一行还在、只是显示上一版。权限比下载严一档:下载认共享智能体的
只读访问,删除只认会话归属人。

Inline references. 正文里的 resource:// 引用渲染成置灰加删除线的卡片,
不可点击,和「模型引用了不存在的文件」的 --pending 态区分开。图片类产物
也降级成卡片:走 renderImage 只会拉取失败,留下一张永远加载不出来的图。
墓碑必须留在传给渲染器的产物列表里——句柄形式与知识库图片同形,过滤掉
之后句柄对不上本条回答,会被当成外部受保护图片去拉一个已经不存在的
blob,结果是裂图。组件里因此拆出 liveArtifacts 只给计数和文件列表用。

Also: 产物库是 offset 分页的无限列表,删一行会让后续页整体前移漏掉一条,
改成按已显示行数取下一页并按 key 去重;access.ResolveMessageArtifact 与
MessageReferencesFile 不再让墓碑授权访问;SessionArtifacts 过滤墓碑,
避免新回答把引用绑到已删文件上;版本澄清文案不再把已删版本算进版本数。

docs/swagger.* 未重新生成:仓库里那份已经落后若干功能,重生成会带进约
1000 行无关漂移,另开 PR 同步。

* fix(artifacts): close the delete holes found in review

Regeneration. loadKnownSet 把墓碑的 ContentHash 也放进了去重集,于是
knownSameContent 会把「删除后技能重新生成、字节恰好相同」当成 fork
checkout 那种 restore:不上传、不挂回本轮回答,文件再也回不来。墓碑现在
只登记 (path, mtime),不进 hash 也不进 byPath —— 前者才是「沙箱文件没动
过就别复活」需要的键,后者是「同内容恢复」的短路,两件事不该共用。
RecordRestoredArtifactMtime 也不再改墓碑:它的 mtime 就是那把锁。

Message file proxy. /sessions/:id/messages/:message_id/files 走
MessageReferencesFile,而正文检查在最前面且无条件为真。删除不会重写回答,
resource:// 句柄还留在正文里,于是这条代理会继续把已删文件交出去——包括
交给共享智能体的只读访客,也包括「字节因为知识库还持有而没被回收」的情形。
现在先看 artifacts:命中本条消息的墓碑直接 false,命中 live 直接 true,
其余(知识库图、附件)才回落到正文。同一文件挂在多个位置时只要还有一个
live 就仍然可取。

Cross-session bytes. CreateForked 复制产物行时连同存储 URL 一起复制,但
不会给副本重新 Bind,所以 catalog 的引用计数看不到分叉。在父会话里删除会
把分叉还在用的字节删掉。无 catalog 部署、以及存的是裸 provider 路径的行,
同样不在计数覆盖内。回收前新增一道 CountLiveArtifactsByURL:只要还有任何
未删除的行指向同一对象就保留字节,查不出来也保留。配套加了 url 的部分索引。

Atomicity. SoftDeleteSessionArtifacts 原来逐条 UPDATE 不带事务,删多版本
时中途失败会留下「一部分已隐藏、字节没回收、接口 500、用户重试 404」。
现在整批放进一个事务。writeMessageArtifacts 是整表替换,拿着删除前快照的
并发写会让墓碑复活而字节已在回收,现在按 position 把已存在的 deleted_at
合并回去——删除是唯一不允许被过期写覆盖的属性。

all_versions 两个路由改用同一个解析器(strconv.ParseBool),此前一个是
== "true"、另一个是 != "false",显式传 1/TRUE 在两边语义不同;缺省值仍
按路由区分。

测试:收集器三条(未动不复活 / 重新生成要回来 / live 的同内容恢复仍短路)、
access 三条(正文不能复活已删授权、多副本只要有 live 就可取、非产物引用
仍走正文)、service 七条(归属人校验、他人会话拒绝、并发认领为空、live
行仍在则保留字节、guard 失败保留字节、多版本按对象归并 owner、无
source_path 不扫全会话)、repository 三条(墓碑抗过期写、事务回滚、跨会话
URL 计数)、handler 一条(两个路由的 all_versions 解析矩阵)。其中
「重新生成要回来」在改回旧实现时确认会失败。

client/ 没有 artifacts 相关 API(只有一个 SSE 事件类型常量),无需同步。
2026-09-20 13:01:46 +08:00
wizardchen a163bab32f feat(knowledge): rework the parse timeline panel's layout and failure card
The processing-pipeline drawer presented a failed parse worst-side-up.
The error card printed the error code twice (headline plus a solid red
pill), gave the most prominent line to the generic "check the logs"
filler, and left the one line that explains the failure — the backend's
own message — last, smallest and greyest. Around it, the waterfall left
roughly half the panel blank, ruled its time axis at 0/124/247/371/494ms
(the raw total cut in four), flung a ROOT/STAGE label to the far right of
every name cell, and drew the stages that an upstream failure had
abandoned as pending rows with dashed stubs pinned to the track edge.

Error card
- Headline says which stage broke; the code appears once, as a quiet
  chip. The backend message is promoted to the card's primary line.
- Retry moves into the card, next to the reason, and loses the
  brand-green outline it wore against a red failure. It stays in the
  toolbar only when a failure carries no last_error.
- The headline no longer uses --td-error-color on the card's own error
  tint, which measured around 3:1 in both themes.

Waterfall
- Axis ticks round onto a 1/2/2.5/5 x 10^n step, and bars are positioned
  against the rounded axis maximum so bars and ruler cannot disagree.
  Gridlines mark each tick behind the bars.
- Kind labels are reserved for `generation` spans instead of stamping
  ROOT/STAGE/SPAN on every row via margin-left:auto.
- Depth is carried by indent guides rather than three near-identical row
  tints that read as banding.
- Stages abandoned by an upstream failure say "not run" and recede.

Layout and interaction
- The detail panel opens on load, on the failed span or the root, so the
  panel is not half empty and the rows are discoverably clickable. It
  stays closed once the user closes it.
- The waterfall is content-sized (capped at 60%) and the detail absorbs
  the remainder, instead of a fixed 50/50 that clipped rows mid-height.
- The dot-joined meta sentence becomes label/value stats; the panel name
  moves to an overline above the filename.

Axis math moves to utils/traceAxis.ts with unit tests. New i18n keys are
added to all five locales; three keys left unreferenced by this change
are removed.
2026-09-20 12:25:31 +08:00
hataa 3017145102
fix(frontend): stop seeding new answer events with prior-round text (#3386)
A new answer round with no tool_call between rounds (stream-stall retry,
length-truncation continuation, empty-answer nudge, finalize synthesis)
inherited the concatenated text of every prior round via the
message.content seed, duplicating it on every recompose with no way to
retract. Seed only when no other live answer event exists in the stream.

Fixes #3383
2026-09-20 12:02:09 +08:00
autumnseasonism 1e4389bb21
fix(wiki): clarify alias matching rules in deduplication prompt (#3418)
Co-authored-by: autumnseasonism <275002454+autumnseasonism@users.noreply.github.com>
2026-09-20 11:24:45 +08:00
Ankit Jha 110134f187
test(im): close the lifecycle test database and keep its name unique (#3433)
* test(im): give each lifecycle test its own sqlite database

newLifecycleTestDB derived its in-memory SQLite DSN from
time.Now().UnixNano() alone. Two tests whose helper calls land in the
same wall-clock tick get the same DSN, and since the database is
cache=shared, the second test's CREATE TABLE collides with the
first's ("table im_channels already exists"). A shared database also
lets one test observe rows created by another.

Every other in-memory test database in this repo names itself from
t.Name() instead (tenant_api_key_test.go, memory/service_test.go,
and others); this helper was the only one still using the wall clock.
Switch to the same convention: t.Name() is unique per test function,
which is all that's needed here since none of the lifecycle tests
call the helper from subtests.

Added TestNewLifecycleTestDBIsolatesConcurrentTests, which runs the
seed and the isolation check as actual parallel subtests rather than
sequentially, closer to the real collision scenario than a plain
sequential pair. Confirmed it fails with the old DSN scheme (fixed
name, forcing the collision) and passes with the fix; ran the full
package 8 times as separate `go test` invocations, matching how the
race is actually triggered, with -race and no failures.

Fixes #3198

Signed-off-by: Ankit Jha <jhaankit373@gmail.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* test(im): close the lifecycle test database and keep its name unique

Review on #3415: switching the DSN to t.Name() fixed the collision between
tests started in the same tick but broke repeated runs. t.Name() is stable
across -count iterations in one process, and newLifecycleTestDB never closed
the sql.DB, so a shared-cache in-memory database outlived the iteration that
created it and the next CREATE TABLE failed with "table im_channels already
exists".

Reproduced with the reported command:

    go test ./internal/im -run '^TestNewLifecycleTestDBIsolatesConcurrentTests$' -count=2

Two changes, both needed:

- t.Cleanup closes the sql.DB immediately after opening, so the database is
  released with the test. Registered before the caller's own cleanups, which
  therefore still run first and can use the database while stopping a service.
- A process-wide sequence number in the name, so a single test calling the
  helper twice gets two databases; those overlap before any cleanup runs, so
  closing alone is not sufficient.

Added the repeated-run check that was asked for, plus one asserting the
connection is actually closed. Verified each half is load-bearing: dropping the
cleanup fails TestNewLifecycleTestDBClosesItsConnection, dropping the sequence
fails TestNewLifecycleTestDBIsUsableTwiceInOneProcess. Package is green at
-count=1 and -count=3 under -race.

---------

Signed-off-by: Ankit Jha <jhaankit373@gmail.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-20 11:24:28 +08:00
Frank_zhu 68e21b6e74
docs(env): warn against SEARXNG_PORT colliding with APP_PORT (#3385)
Document that binding SearXNG to the same port as the app can make
localhost hit SearXNG's HTML 404 on login. Docs-only; no runtime change.
2026-09-20 11:14:42 +08:00