chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

@@ -0,0 +1,93 @@
id: no-new-base-model
language: python
severity: error
message: New SQLAlchemy models must inherit from TypeBase instead of Base.
utils:
project-import:
kind: import_from_statement
has:
field: module_name
regex: '^(models\.base|api\.models\.base|\.+base)$'
project-base:
any:
- all:
- pattern: Base
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- matches: project-import
- has:
stopBy: neighbor
all:
- kind: dotted_name
- regex: '^Base$'
- regex: '^(api\.)?models\.base\.Base$'
- all:
- pattern: $BASE
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern:
context: from models.base import Base as $BASE
selector: aliased_import
- inside:
stopBy: neighbor
matches: project-import
- all:
- pattern: $MODULE.Base
- inside:
kind: module
stopBy: end
has:
stopBy: end
any:
- all:
- pattern:
context: import models.base as $MODULE
selector: aliased_import
- has:
field: name
regex: '^(api\.)?models\.base$'
- all:
- pattern:
context: from models import base as $MODULE
selector: aliased_import
- inside:
kind: import_from_statement
stopBy: neighbor
has:
field: module_name
regex: '^(api\.)?models$'
- all:
- pattern: base.Base
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- kind: import_from_statement
- has:
field: module_name
regex: '^(api\.)?models$'
- has:
stopBy: neighbor
all:
- kind: dotted_name
- regex: '^base$'
rule:
all:
- kind: class_definition
- has:
field: name
pattern: $NAME
- has:
field: superclasses
has:
matches: project-base
@@ -0,0 +1,84 @@
id: no-new-cast
language: python
severity: error
message: Use type narrowing or validate values instead of typing.cast().
utils:
typing-import:
kind: import_from_statement
has:
field: module_name
regex: '^(typing|typing_extensions)$'
cast-reference:
any:
- pattern: typing.cast
- pattern: typing_extensions.cast
- all:
- pattern: $MODULE.cast
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- any:
- pattern:
context: import typing as $MODULE
selector: aliased_import
- pattern:
context: import typing_extensions as $MODULE
selector: aliased_import
- has:
field: name
regex: '^(typing|typing_extensions)$'
- all:
- kind: identifier
- pattern: cast
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- matches: typing-import
- has:
stopBy: neighbor
all:
- kind: dotted_name
- regex: '^cast$'
- all:
- kind: identifier
- pattern: $IMPORTED
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern:
context: from typing import cast as $IMPORTED
selector: aliased_import
- inside:
matches: typing-import
stopBy: neighbor
rule:
all:
- kind: call
- any:
- has:
field: function
matches: cast-reference
- all:
- has:
field: function
kind: identifier
pattern: $ALIAS
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern: $ALIAS = $ORIGINAL
- has:
field: right
matches: cast-reference
@@ -0,0 +1,56 @@
id: no-new-constructor-bypass
language: python
severity: error
message: Use the class constructor to preserve initialization and validation.
utils:
allocation-in-new:
all:
- any:
- pattern: $OWNER.__new__($CLS)
- pattern: $OWNER.__new__($CLS, $$$TAIL)
- inside:
all:
- kind: function_definition
- has:
field: name
regex: '^__new__$'
- has:
field: parameters
has:
all:
- nthChild: 1
- any:
- all:
- kind: typed_parameter
- has:
pattern: $CLS
stopBy: neighbor
- pattern: $CLS
stopBy: neighbor
- inside:
kind: class_definition
stopBy:
any:
- kind: function_definition
- kind: lambda
- kind: class_definition
stopBy:
any:
- kind: function_definition
- kind: lambda
- kind: class_definition
rule:
any:
- all:
- pattern: $OWNER.__new__($$$ARGS)
- not:
matches: allocation-in-new
- all:
- pattern: $OWNER.__new__
- not:
inside:
kind: call
has:
field: function
pattern: $OWNER.__new__
stopBy: neighbor
@@ -0,0 +1,54 @@
id: no-new-controller-sqlalchemy
language: python
rule:
any:
- pattern: db.session.add($$$ARGS)
- pattern: db.session.add_all($$$ARGS)
- pattern: db.session.begin($$$ARGS)
- pattern: db.session.commit($$$ARGS)
- pattern: db.session.delete($$$ARGS)
- pattern: db.session.execute($$$ARGS)
- pattern: db.session.flush($$$ARGS)
- pattern: db.session.get($MODEL, $$$ARGS)
- pattern: db.session.merge($$$ARGS)
- pattern: db.session.refresh($$$ARGS)
- pattern: db.session.rollback($$$ARGS)
- pattern: db.session.scalar($$$ARGS)
- pattern: db.session.scalars($$$ARGS)
- pattern: session.add($$$ARGS)
- pattern: session.add_all($$$ARGS)
- pattern: session.begin($$$ARGS)
- pattern: session.commit($$$ARGS)
- pattern: session.delete($$$ARGS)
- pattern: session.execute($$$ARGS)
- pattern: session.flush($$$ARGS)
- pattern: session.get($MODEL, $$$ARGS)
- pattern: session.merge($$$ARGS)
- pattern: session.refresh($$$ARGS)
- pattern: session.rollback($$$ARGS)
- pattern: session.scalar($$$ARGS)
- pattern: session.scalars($$$ARGS)
- pattern: Session($$$ARGS)
- pattern: sessionmaker($$$ARGS)
- pattern: select($$$ARGS)
- pattern: insert($$$ARGS)
- pattern: update($$$ARGS)
- pattern: delete($$$ARGS)
- pattern: text($$$ARGS)
- pattern: sa.select($$$ARGS)
- pattern: sa.insert($$$ARGS)
- pattern: sa.update($$$ARGS)
- pattern: sa.delete($$$ARGS)
- pattern: sa.text($$$ARGS)
- pattern: sqlalchemy.select($$$ARGS)
- pattern: sqlalchemy.insert($$$ARGS)
- pattern: sqlalchemy.update($$$ARGS)
- pattern: sqlalchemy.delete($$$ARGS)
- pattern: sqlalchemy.text($$$ARGS)
- pattern: db.select($$$ARGS)
- pattern: db.insert($$$ARGS)
- pattern: db.update($$$ARGS)
- pattern: db.delete($$$ARGS)
- pattern: db.text($$$ARGS)
message: net-new direct SQLAlchemy call in controller code
severity: error
@@ -0,0 +1,12 @@
id: no-new-getattr
language: python
rule:
any:
- pattern: getattr($OBJ, $NAME)
- pattern: getattr($OBJ, $NAME, $$$REST)
- pattern: builtins.getattr($OBJ, $NAME)
- pattern: builtins.getattr($OBJ, $NAME, $$$REST)
- pattern: __builtins__.getattr($OBJ, $NAME)
- pattern: __builtins__.getattr($OBJ, $NAME, $$$REST)
message: net-new getattr() in added code
severity: error
@@ -0,0 +1,61 @@
id: no-new-mock
language: python
severity: error
message: Use a real instance instead of adding a Mock, MagicMock, or SimpleNamespace constructor.
rule:
all:
- pattern: $CALLEE($$$ARGS)
- any:
- has:
field: function
stopBy: end
regex: '(^|\.)(Mock|MagicMock|SimpleNamespace)$'
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern:
context: from package import $ORIGINAL as $CALLEE
selector: aliased_import
- any:
- all:
- has:
field: name
stopBy: end
regex: '^(Mock|MagicMock)$'
- inside:
kind: import_from_statement
stopBy: end
has:
field: module_name
stopBy: end
regex: '^unittest\.mock$'
- all:
- has:
field: name
stopBy: end
regex: '^SimpleNamespace$'
- inside:
kind: import_from_statement
stopBy: end
has:
field: module_name
stopBy: end
regex: '^types$'
- all:
- has:
field: function
kind: identifier
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern: $CALLEE = $ORIGINAL
- has:
field: right
stopBy: end
regex: '(^|\.)(Mock|MagicMock|SimpleNamespace)$'
@@ -0,0 +1,11 @@
id: no-new-session-mock
language: python
rule:
any:
- pattern: $SESSION = MagicMock($$$ARGS)
- pattern: $SESSION = Mock($$$ARGS)
constraints:
SESSION:
regex: ^(session|db_session|mock_session|read_session|write_session)$
message: net-new session mock assignment in test code
severity: error
@@ -0,0 +1,98 @@
id: no-new-stub-subclass
language: python
severity: error
message: Instantiate the production runtime class; do not bypass its constructor in a test subclass.
utils:
runtime-base:
any:
- regex: '(^|\.)(ModelInstance|ModelManager|DatasetRetrieval|TraceQueueManager|MessageBasedAppQueueManager|Thread)$'
- all:
- pattern: $BASE
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern:
context: from package import $ORIGINAL as $BASE
selector: aliased_import
- has:
field: name
stopBy: end
regex: '^(ModelInstance|ModelManager|DatasetRetrieval|TraceQueueManager|MessageBasedAppQueueManager|Thread)$'
initializer:
kind: function_definition
has:
field: name
stopBy: end
regex: ^__init__$
delegates:
has:
# Nested definitions do not initialize the containing object.
stopBy:
any:
- kind: function_definition
- kind: class_definition
- kind: lambda
any:
- pattern: super($$$ARGS).__init__($$$PARAMS)
- pattern: $BASE.__init__(self, $$$PARAMS)
dataclass-decorator:
kind: decorator
regex: '^@(dataclasses\.)?dataclass(\(|$)'
not:
has:
stopBy: end
pattern:
context: dataclass(init=False)
selector: keyword_argument
rule:
all:
- kind: class_definition
- has:
field: name
stopBy: neighbor
pattern: $NAME
- has:
field: superclasses
stopBy: neighbor
has:
stopBy: neighbor
all:
- pattern: $BASE
- matches: runtime-base
- any:
- has:
field: body
stopBy: neighbor
has:
# Only methods of this class, not constructors of nested classes.
stopBy: neighbor
all:
- matches: initializer
- not:
matches: delegates
- all:
- inside:
# A nested class must not inherit its outer class's decorator.
stopBy: neighbor
kind: decorated_definition
has:
stopBy: neighbor
matches: dataclass-decorator
- not:
has:
field: body
stopBy: neighbor
has:
stopBy: neighbor
any:
- matches: initializer
- all:
- kind: function_definition
- has:
field: name
stopBy: end
regex: ^__post_init__$
- matches: delegates
@@ -0,0 +1,63 @@
id: no-spec-mock
language: python
severity: error
message: Use a real instance or a concrete test fake instead of a spec-based mock.
rule:
all:
- pattern: $CALLEE($$$ARGS)
# Inspect direct arguments only; nested calls may have unrelated spec keywords.
- has:
field: arguments
has:
any:
- all:
- kind: keyword_argument
- has:
field: name
regex: ^spec(_set)?$
- all:
- kind: dictionary_splat
- has:
kind: dictionary
has:
kind: pair
has:
field: key
regex: ^['"]spec(_set)?['"]$
- not:
any:
- kind: keyword_argument
- kind: dictionary_splat
- kind: comment
- regex: '^[(),]$'
- any:
- has:
field: function
regex: '(^|\.)(Mock|MagicMock|AsyncMock|NonCallableMock|NonCallableMagicMock)$'
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern:
context: from unittest.mock import $ORIGINAL as $CALLEE
selector: aliased_import
- has:
field: name
regex: '^(Mock|MagicMock|AsyncMock|NonCallableMock|NonCallableMagicMock)$'
- inside:
kind: import_from_statement
has:
field: module_name
regex: '^unittest\.mock$'
- inside:
kind: module
stopBy: end
has:
stopBy: end
all:
- pattern: $CALLEE = $ORIGINAL
- has:
field: right
regex: '(^|\.)(Mock|MagicMock|AsyncMock|NonCallableMock|NonCallableMagicMock)$'